The SharePoint Service Locator

Service location is an application pattern where a programmable run-time component locates implementations of a user-specified interface. You can think of the service locator as a table that is available at run time. The table maps an interface and an optional key string to the name of a class that implements the given interface. The following table illustrates this. Each row in the table is a type mapping.

Key Lookup Value
(ILogger, null) TraceLogger
(ILogger, "debug") DebugTraceLogger
(IProductCatalogRepository, null) ProductCatalogRepository


For example, suppose that at run time you need to find a service that supports the ILogger interface. You invoke the service locator and the service locator responds by looking up a matching implementation class in its table of type mappings. By default, the corresponding implementation class is TraceLogger. If you give the string "debug" as an additional, optional lookup key, the corresponding class is DebugTraceLogger. After it looks up the correct class, the service locator returns an instance of that class for you to use.

The service locator defers the association of interfaces to implementation classes from compile time to run time. This additional level of indirection provides a number of benefits to applications and their test environments. See [TBD] for more information on the service locator pattern.

The SharePoint Guidance Library provides a service locator that is specialized for the SharePoint environment. The SharePoint service locator is a reusable component that you can include in your own SharePoint applications. The SharePoint service locator allows you to configure your application's service types when your application is installed. You may want to do this, for example, when a SharePoint feature receiver executes. For more information, see Adding a service to an application's configuration. You can also use the SharePoint service locator to support unit testing. For more information, see Replacing the current service locator.

This section includes the following topics:
  • Key Scenarios
  • Design of the SharePoint Service Locator
  • Development HowTos and Quickstarts



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

Last edited Jun 16, 2009 at 2:35 PM by ckeyser, version 1

Comments

No comments yet.