Share via


Providing a Custom Service Locator Implementation

Typical Goals

In some circumstances, you might require a service locator implementation that provides more sophisticated features than those offered by the SharePoint Service Locator's default implementation. For example, you want to develop a service locator that automatically selects the most appropriate implementation of an interface according to run-time conditions. Alternatively, you might require a service locator implementation that employs a dependency injection pattern.

Solution

The SharePoint Service Locator allows you to substitute the default service locator implementation with other IServiceLocator implementations. You can use the following high-level steps to do this.

  1. Create a class that implements the IServiceLocatorFactory interface.
  2. In the Create method, instantiate and return your custom service locator instance.
  3. In the LoadTypeMappings method, add the passed-in type mappings to the passed-in service locator instance.
  4. In the Current service locator instance provided by the SharePoint Service Locator, add a type mapping that associated your implementation with the IServiceLocatorFactory interface.

In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SharePoint.Common.dll, and to Microsoft.Practices.ServiceLocation.dll. Create a class that implements the IServiceLocatorFactory interface and creates the custom service locator instance. Register your new class factory using the IServiceLocatorConfig interface**.**

Using a Custom Service Locator

The following code shows how you can create a new implementation of the IServiceLocatorFactory implementation. The code assumes that you have created a custom service locator named MyServiceLocator that implements the IServiceLocator interface.

class MyServiceLocatorFactory : IServiceLocatorFactory
{
   public IServiceLocator Create()
   {
      return new MyServiceLocator();
   }

   public void LoadTypeMappings(IServiceLocator serviceLocator, 
                                IEnumerable<TypeMapping> typeMappings)
   {
      if (serviceLocator == null)
      {
         throw new ArgumentNullException("serviceLocator");
      }

      MyServiceLocator myServiceLocator = serviceLocator as MyServiceLocator;

      if (typeMappings == null)
      {
         return;
      }

      foreach(TypeMapping typeMapping in typeMappings)
      {
         // ... invoke custom methods of MyServiceLocator to establish type  
         //mappings
      }
   }
}

Now that you have created your IServiceLocatorFactory implementation, you must register this implementation with the SharePoint Service Locator. The following code shows you how to do this. Typically, you should include this code in a feature receiver class.

The following code example shows how to configure the SharePoint Service Locator to use your custom service locator factory.

IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
IServiceLocatorConfig typeMappings = 
 serviceLocator.GetInstance<IServiceLocatorConfig>();
typeMappings.RegisterTypeMapping<IServiceLocatorFactory,    
 MyServiceLocatorFactory>();

SharePointServiceLocator.Reset();

Usage Notes

The IServiceLocatorFactory.Create method must return an instance of a class that implements the IServiceLocator interface. The IServiceLocator interface is defined by the Common Service Locator library, which is available on CodePlex.

The LoadTypeMappings method initializes the service locator instance that is returned by the Create method with application-specific type mappings that are provided as arguments. The LoadTypeMapping method must add each type mapping to your service locator's table of type mappings.

In some circumstances the LoadTypeMappings method can be invoked more than once. In the current version of the SharePoint Guidance Library, this method is invoked once for the default type mappings for the SharePoint Guidance Library and once for type mappings that are stored in farm configuration for the farm level service locator. For the combined site and farm-scoped service locator, LoadTypeMappings is invoked one additional time for the type mappings that are stored in site collection configuration settings. Your implementation of the LoadTypeMappings method must be able to overwrite any previous type mappings and ensure that the most recent type mapping takes precedence.