EasyLocator

EasyLocator is an easy IoC Container that implements IServiceLocator (Microsoft.Practices.ServiceLocation).
It allows you to register Services and instances.
  • Named registering : You can name Services and instances if you want to store several instances of a type or retrieve them easily.
  • Caching Strategy : by default you get new instances every time you try to get one. You can also keep alive instances and cache them.
  • EasyLocator uses Reflection to Resolve not registered types . Also registering ViewModels for example is not required. EasyLocator registers them and instanciates them for you . Only registering Services is really required (pair interface + implementation for example) because it can't resolves the "preferred" implementation .

You can download from CodePlex or use Packages NuGet .

Registering a Service

  • With Static member
 EasyLocator.Default.Register<IService, Service>();
  • Or creates a new instance of EasyLocator (EasyLocator implements ILocator, IServiceLocator and IServiceProvider)
var container = new EasyLocator();
container.Register<IService, Service>();
  • Registering a named Service
EasyLocator.Default.Register<IService, Service>("serviceOne");
EasyLocator.Default.Register<IService, OtherService>("serviceTwo");
  • Caching Strategy : Keep alive an instance
EasyLocator.Default.Register<ProductsViewModel>(Lifetime.Singleton);
  • Multiples registrations
var container = new EasyLocator();
container.Register<IService, Service>()
                .Register<MainViewModel>()
                .RegisterInstance<Product>(new Product("p1"));
  • With a base class
 EasyLocator.Default.Register<Person, Employee>();

Retrieving a service

 var instance = EasyLocator.Default.GetInstance<IService>();

 var instance = EasyLocator.Default.GetInstance<Person>();
  • Retrieving a named Service
 var instance = EasyLocator.Default.GetInstance<IService>("serviceTwo");

Registering an instance

 var p1= new Product();
EasyLocator.Default.RegisterInstance<Product>(p1);
  • Registering several instances of a same type and name them
 var p1= new Product();
 var p2= new Product();

EasyLocator.Default.RegisterInstance<Product>(p1);
EasyLocator.Default.RegisterInstance<Product>(p2, "productTwo");
  • Other samples : with Static Method
EasyLocator.Default.RegisterInstance<Employee>(Employee.CreateEmployee()); 
  • with Enum
EasyLocator.Default.RegisterInstance(DateTimeKind.Utc); 

Retrieving an instance

var  instance = EasyLocator.Default.GetInstance<Product>();
  • Retrieving an instance by name
var  instance = EasyLocator.Default.GetInstance<Product>("productTwo");

ServiceLocatorProvider

  • Setting up the Service Locator provider
 ServiceLocator.SetLocatorProvider(() => EasyLocator.Default);
  • Registering a service with EasyLocator
 EasyLocator.Default.Register<IService, Service>();
  • And finally retrieving it with Microsoft.Practices.ServiceLocation.ServiceLocator
var service = ServiceLocator.Current.GetInstance<IService>();

Blog

Last edited Sep 17, 2014 at 10:22 AM by romagny13, version 29