Service Locator
Dependency Injection is an way to separate dependency. Someone can use Constructor Injection, Setter Injection etc for injecting dependency. Service Locator solves the same problem as Dependency Injector but it uses different approach. Service Locator holds reference of all types/objects an application needs. So whenever you need to use an instance of a type/class, ask service locator to provide the instance. Simply, the Service Locator uses a container to hold all references to class/type to instance/object mapping. You can follow the links for details:Why Common Service Locator?
Nowadays as a .NET developer, we have a set of DI/IoC containers to choose from. We have option for Spring.NET, Unity Application Block, Castle Windsor etc. Whenever you use any of them in your project, the project is tightly coupled with that container. So if you start your project with Spring.Net Container then your project will be tightly coupled with the Spring.Net container. But later if your decide to use Unity Application Block then can you imagine how much hassles are waiting for you? You need to remove all reference of Spring.Net container from your code. Another example might be, you are developing a library that will be used by other users. Now if you use a IoC Container (say Spring.Net) then you are forcing users to use Spring.Net whenever they want to use the library. So if it would be possible to loose the coupling between your code and the IoC container then you can easily remove dependencies on IoC container.The Common Service Locator project at codeplex is aimed to attain the goal.Use Common Service Locator
1. Get an understanding of IServiceLocator interface
The Start with an Interface IServiceLocator which has few methods which is generic to any IoC container.The IServiceLocator inherits from IServiceProvider.public interface IServiceProvider
{
object GetService(Type serviceType);
}
public interface IServiceLocator : IServiceProvider
{
object GetInstance(Type serviceType);
object GetInstance(Type serviceType, string key);
IEnumerable<object> GetAllInstances(Type serviceType);
TService GetInstance<TService>();
TService GetInstance<TService>(string key);
IEnumerable<TService> GetAllInstances<TService>();
}
- DoGetAllInstances: get all instance of a particular type from Container.
- DoGetInstance: Get a particular instance from Container
2. Develop your custom Service Locator
I have developed two service Locators for Unity and Spring.Net. UnityServiceLocator can use Unity container whereas Spring.Net can uses Object Factory. The following class diagram shows two implementations that I have developed in the project attached with this post.Figure: Two sample implementations of Common Service Locator
3. Develop BootStrapper
So I have developed two service locators till now. Now I need to develop two bootstrappers for Spring.Net and Unity as type registration are different for two.public abstract class CommonBootStrapper
{
public static IServiceLocator Locator;
protected CommonBootStrapper()
{
Locator = CreateServiceLocator();
}
protected abstract IServiceLocator CreateServiceLocator();
}
public class UnityBootStrapper : CommonBootStrapper
{
protected override IServiceLocator CreateServiceLocator()
{
UnityContainer container = new UnityContainer();
RegisterTypes(container);
return new UnityServiceLocator(container);
}
private static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<ICustomer, GoldenCustomer>(typeof(ICustomer).FullName);
}
}
4. Develop a BootStrapper Manager to provide a single point of access to Service Locator
Finally you need to develop a bootstrapper manager to hide the concrete implementation of CommonBootStrapper. The following code shows the BootStrapperManagerpublic class BootStrapperManager
{
private static CommonBootStrapper _bootStrapper;
public static void Initialize(CommonBootStrapper bootStrapper)
{
_bootStrapper = bootStrapper;
}
public static CommonBootStrapper BootStrapper
{
get { return _bootStrapper; }
}
}
5. Access the IServiceLocator through BootStrapperManager
Now you need to register your BootStrapper to BootStrapperManager at some entry point to your application (like Main method). I have register as shown below:BootStrapperManager.Initialize(new UnityBootStrapper());
ICustomer customer = CommonBootStrapper.Locator.GetInstance<ICustomer>(typeof(ICustomer).FullName);
customer.SendMessage("This is me.");
Do you mean: Sprint.net is Spring.net?
ReplyDeleteI think it is type error ^_^
Yes I meant Sprint.net.Sorry for this typo.
ReplyDeleteWhen I try to download the file, I got file not exists error:
ReplyDelete"This item might not exist or is no longer available"
Has the file been removed?