Service Locator pattern in SharePoint

 When we develop a class ,the code will depend on other classes for providing functionality. The result of this being a tight coupling between the consumer class and the dependency class.
 
Following are the issues with the above dependencies 

  • As the code base becomes more complex, it can be difficult to manage references and assembly as the dependences are scattered throughout various projects and solutions .If we modify one class, we must recompile every project that references the class.
  • Unit testing becomes complicated   

We can overcome this by using the interface to provide a foundation for the dependency class. we can then change or replace the interface implementation without altering how the service is consumed.

With the above approach we can resolve the issue of consumer class being independent on the dependency class,but the consumer class has to still create the instance of the dependency class .This is overcome using the service locator

In service locator, our code can create and use an instance of the class that supports a desired interface without knowing the name or any details of the class that implements the interface .This removes the dependency between the consumer class and the implementation class (dependency class).

The SharePoint Service Locator is a simple, easy-to-use implementation of the Service Locator pattern. At the core of the SharePoint Service Locator is a dictionary of type mappings. Each dictionary entry maps an interface and an optional key string to the name of a class that implements the specified interface. The following table illustrates this. These type mappings are included by default to support other components in the SharePoint Guidance library

Interface

Registered implementation class

ILogger

SharePointLogger

IHierarchicalConfig

HierarchicalConfig

IConfigManager

ConfigManager

Class using the SharePoint Service Locator to retrieve a service

http://i.msdn.microsoft.com/dynimg/IC416563.png

This is how the client invokes the services:

IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
ILogger logger = serviceLocator.GetInstance<ILogger>();

The generic C# implementation  details are as below
//contract of a service locator
public interface IServiceLocator

{

    ``T GetService<T>();

}

//Service implementation
class ServiceLocator : IServiceLocator

{

    ``// map that contains pairs of interfaces and

    ``// references to concrete implementations

    ``private IDictionary<``object``, ``object``> services;

 

    ``internal ServiceLocator()

    ``{

        ``services = ``new Dictionary<``object``, ``object``>();

 

        ``// fill the map

        ``this``.services.Add(``typeof``(IServiceA), ``new ServiceA());

        ``this``.services.Add(``typeof``(IServiceB), ``new ServiceB());

        ``this``.services.Add(``typeof``(IServiceC), ``new ServiceC());

    ``}

 

    ``public T GetService<T>()

    ``{

        ``try

        ``{

            ``return (T)services[``typeof``(T)];

        ``}

        ``catch (KeyNotFoundException)

        ``{

            ``throw new ApplicationException(``"The requested service is not registered"``);

        ``}

    ``}

}

This is how the client invokes the code:

IServiceLocator locator = ``new ServiceLocator();

IServiceA myServiceA = locator.GetService<IServiceA>();

References
http://msdn.microsoft.com/en-us/library/ff798368.aspx
http://stefanoricciardi.com/2009/09/25/service-locator-pattern-in-csharpa-simple-example/