Share via


Creating a Custom Service Locator

Typical Goals

In this scenario, you want to configure your application to use a service locator implementation other than the one provided by the SharePoint Guidance Library.

Solution

In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SPG.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 procedure describes how to use a custom service locator.

To use a custom service locator

  1. Create a ServiceLocatorFactory class that will instantiate the custom service locator and populate it with type mappings.
  2. Register the ServiceLocatorFactory implementation in the ServiceLocatorConfig class.

The following code example declares a new implementation of the IServiceLocatorFactory 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
      }
   }
}

In this example, the MyServiceLocator class represents the custom service locator implementation you want to instantiate.

The following code example shows how to configure your application to use your custom service locator factory.

IServiceLocator serviceLocator = SharePointServiceLocator.Current;
IServiceLocatorConfig typeMappings =  
                                 serviceLocator.GetInstance<IServiceLocatorConfig>();

typeMappings.RegisterTypeMapping<IServiceLocatorFactory, MyServiceLocatorFactory>();

Usually, the preceding code runs inside a feature receiver.

Usage Notes

The Create method must return an instance of a class that adheres to the contract that is specified by the Common Service Locator's IServiceLocator interface.

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

It is possible that 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 configuration as SharePoint properties. Your implementation of the LoadTypeMappings method must be able to overwrite the previous type mappings and ensure that the most recent type mapping takes precedence.

Home page on MSDN | Community site