This project is read-only.

Adding a Service to an Application's Configuration

Typical Goals

In this scenario, you want to modify the application's configuration with a new mapping of an interface to an implementation class. For example, you want to register the IPricingRepository interface to the PricingRepository class. This scenario is typical when implementing a SharePoint feature receiver.


In Visual Studio, add a reference to the SharePoint Guidance Library, Microsoft.Practices.SPG.Common.dll. Use the SharePoint service locator to get a reference to a Microsoft.Practices.SPG.Common.ServiceLocation.IServiceLocatorConfig interface and invoke the RegisterTypeMapping method. Use the interface and the implementation class you want your application class to use at run time as type parameters to the RegisterTypeMapping method.


For an extended example of how to add a service to an application's configuration, see [TBD].

Adding a Service to an Application's Configuration

The following code shows how to add a service to an application's configuration from within a feature receiver class.
using Microsoft.Practices.SPG.Common.ServiceLocation; 

public class MyFeatureReceiver : SPFeatureReceiver
    /// ...

    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
    public override void FeatureInstalled(SPFeatureReceiverProperties properties)
       IServiceLocator serviceLocator = SharePointServiceLocator.Current;
       IServiceLocatorConfig typeMappings =  

       typeMappings.RegisterTypeMapping<IPricingRepository, PricingRepository>();

The code declares a new feature receiver class named MyFeatureReceiver. Its FeatureInstalled method registers the PricingRepository class as the configured implementation of the IPricingRepository interface. SharePoint will invoke the FeatureInstalled method after the feature is installed.

For more information about using feature receivers, see TBD.

Usage Notes

The RegisterTypeMapping method has three overloaded versions. You can use these to provide a name for the type mapping and to indicate whether a new instance of the implementation class is provided with every service location request or whether a single shared instance is used for all requests. By default, a new instance is created with every service location request.

See Getting Services from the SharePoint Service Locator for an example of how to get a service instance based on a registered type mapping.

To give feedback.
Copyright (c) 2007 by Microsoft Corporation. All rights reserved.

Last edited Jun 16, 2009 at 3:47 PM by ckeyser, version 1


No comments yet.