Partager via


Adding Type Mappings

To register a service with the SharePoint Service Locator, you must add a type mapping. To add a type mapping to the current service locator instance, you should use the SharePoint Service Locator to get a reference to a Microsoft.Practices.SharePoint.Common.ServiceLocation.IServiceLocatorConfig interface, and then call the RegisterTypeMapping method on the IServiceLocatorConfig object.

The following code shows how you can register a class named Service1 as an implementation of the IService1 interface (as a farm-scoped type mapping).

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

typeMappings.RegisterTypeMapping<IService1, Service1>();

If you want to register or change a type mapping for a site collection, you must first set the Site property on the IServiceLocatorConfig instance. This approach of setting context for the service locator configuration class is an example of property injection. The following code shows how you can register a type mapping at the site collection level.

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

typeMappings.RegisterTypeMapping<IService1, Service1>();

If you want to add a name to the type mapping, you can include a key string as a parameter to the RegisterTypeMapping method.

typeMappings.RegisterTypeMapping<IService1, AltService1>("alternate");

For more information on retrieving services by name, see Retrieving Service Instances.

Note

The RegisterTypeMapping method stores type mappings in the farm-level property bag. SharePoint 2010 does not allow you to write to the farm-level property bag from content Web applications—the entire Microsoft.SharePoint.Administration is blocked for security reasons. Instead, you must perform this operation from a feature receiver, a command line application, or the Central Administration Web application.

Typically, you would add type mappings from within a feature receiver class. When the feature containing your interface implementation is activated, the type mapping is added to the SharePoint Service Locator. For more information, see the scenario topic Using a Feature Receiver to Register a Type Mapping.

The SharePoint Service Locator can instantiate services in two different ways. By default, it creates a new service instance for each request. However, you can also configure type mappings such that the service locator will create a singleton service—in other words, that service will be instantiated once, and that service instance will be shared by all callers.

When you use the IServiceLocatorConfig.RegisterTypeMapping method to add a type mapping, the SharePoint Service Locator will always create a new service instance every time that the service is requested. You cannot change this behavior. This is because in most circumstances you should not use the service locator to register types as singletons. Registering a service as a singleton requires the service to be thread-safe. Unless you have designed your service to be thread safe, you should not share a single instance of your object across all application threads.

In certain scenarios, such as unit testing, you may need to register a service as a singleton. In this case, you must use the ActivatingServiceLocator class directly. The ActivatingServiceLocator class extends the IServiceLocator interface in two important ways:

  • It allows you to register type mappings directly, rather than through an IServiceLocatorConfig implementation.
  • It allows you to specify whether types should be instantiated as singleton services.

To register a type as a singleton service, first cast the current service locator instance to the ActivatingServiceLocator type. Next, call the RegisterTypeMapping method and pass in an InstantiationType.AsSingleton enumeration value as a parameter.

ActivatingServiceLocator serviceLocator = 
 (ActivatingServiceLocator)SharePointServiceLocator.GetCurrent(); 
serviceLocator.RegisterTypeMapping<IService2, 
Service2>(InstantiationType.AsSingleton);

You can also add a name to the type mapping by including a key string parameter.

serviceLocator.RegisterTypeMapping<IService2, AltService2> ("alternate", 
InstantiationType.AsSingleton);

For more information on the ActivatingServiceLocator class, see Design of the SharePoint Service Locator.