The SharePoint Service Locator

What do we mean by service when we say Service Locator? We don't necessarily mean a Web service. In this context, a service is any class that provides a service to other classes. The services provided by each class are defined by the interfaces that the class implements.

Note

This documentation makes frequent references to implementations and implementation classes. These terms are used to refer to classes that implement an interface required by the caller.

Service location takes this decoupling process to the next level. When you use a service location pattern, your code can create and use an instance of a class that supports a desired interface without knowing the name or any details of the class that implements the interface. This removes the dependency between the consumer class and the implementation class. For more information on the service locator pattern, see The Service Locator Pattern on MSDN.

The SharePoint Service Locator is a reusable component that provides a simple implementation of the service locator pattern. You can use it in your own SharePoint applications to decouple the consumers of an interface from the implementations of that interface. Instead of creating an object by invoking the constructor of a class, you request an object with a specified interface from the service locator. The service implementation can now be replaced or updated without altering the consuming class implementation.

So why is it a good idea to remove explicit dependencies on other classes from your code? As your code base becomes more complex and abstracted, it can be difficult to manage references and assembly dependencies that are scattered throughout various projects and solutions. These dependencies often make it challenging to maintain code over timeā€”if you modify one class, you must recompile every project that references that class. This also makes unit testing code in isolation much more complicated. In short, decoupling your code from specific types makes your code more modular, easier to manage, and easier to test.

This section includes the following topics that will help you to understand and use the SharePoint Service Locator:

  • What Does the SharePoint Service Locator Do? This section provides a brief overview of the SharePoint Service Locator, the concepts behind it, and the features that it incorporates. It also provides a simple example of how you can programmatically interact with the SharePoint Service Locator.
  • When Should I Use the SharePoint Service Locator? This section can help you to decide whether the SharePoint Service Locator is suitable for your requirements. It identifies key scenarios in which the SharePoint Service Locator may be useful to you, and it explains the benefits and limitations of using the SharePoint Service Locator.
  • Developing Applications Using the SharePoint Service Locator. This section describes how to approach the key development tasks that you will encounter when you use the SharePoint Service Locator, such as adding the right references to your solution, getting a service instance, and adding or removing type mappings.
  • Key Scenarios. This section provides some examples of the SharePoint Service Locator at work in the context of broader SharePoint operations.
  • Design of the SharePoint Service Locator. This section explains how the SharePoint Service Locator works in more detail, including the background and rationale behind the component.
  • Developer How-To Topics. This section provides step-by-step, end-to-end procedural guidance on how to perform the most common developer tasks with the SharePoint Service Locator.