Бөлісу құралы:


Using a Feature Receiver to Register a Type Mapping

Typical Goals

When you write a class that implements a particular interface, you can use the SharePoint Service Locator to make your class available to callers who require an implementation of that interface. In this example, suppose you have created a new implementation of the IPricingRepository interface named PriceRepUltimate. You have also created a SharePoint feature to deploy the new component to a SharePoint environment.

Solution

In most cases, you should create a feature receiver class and override the FeatureInstalled method to register a new type mapping with the SharePoint Service Locator. By registering the type mapping within the FeatureInstalled method, you ensure that the type mapping is added when, and only when, your component is made available to the SharePoint environment.

For more information on how to register type mappings, including how to create named type mappings and how to specify that types should be instantiated as singleton services, see Adding Type Mappings.

For more information about using feature receivers, see Using Features on MSDN.

Registering a Service in a Feature Receiver Class

The following code shows how to use the RegisterTypeMapping method to retrieve the service instance. This example assumes that you have added references to the Microsoft.Practices.SharePoint.Common.dll and Microsoft.Practices.ServiceLocation.dll assemblies.

using Microsoft.SharePoint;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.SharePoint.Common.ServiceLocation; 

[Guid("8b0f085e-72a0-4d9f-ac74-0038dc0f6dd5")]
public class MyFeatureReceiver : SPFeatureReceiver
{ 
    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
    {
       // Get the ServiceLocatorConfig service from the service locator.
       IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
       IServiceLocatorConfig typeMappings =  
                             serviceLocator.GetInstance<IServiceLocatorConfig>();

       typeMappings.RegisterTypeMapping<IPricingRepository, PriceRepUltimate>();
    }   
}

The FeatureInstalled method registers the PriceRepUltimate class as the configured implementation of the IPricingRepository interface. You should avoid using FeatureActivated method here since it may not be running at a high enough permission level to update type mapping.

Usage Notes

  • Typically you should unregister the type mapping when the feature is uninstalled, because the assembly that includes the implementation is made unavailable at this point. You can include this functionality in your feature receiver class by overriding the FeatureUninstalling method, as illustrated by the following code.
public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
{
 // Get the ServiceLocatorConfig service from the service locator.
 IServiceLocator serviceLocator = SharePointServiceLocator.GetCurrent();
 IServiceLocatorConfig typeMappings =  
                            serviceLocator.GetInstance<IServiceLocatorConfig>();

 typeMappings.RemoveTypeMapping<IPricingRepository>(null);
}

For more information on how to remove type mappings, see Removing Type Mappings.