. NET Server location mode (Service Locator Pattern), locatorpattern

Source: Internet
Author: User

. NET Server location mode (Service Locator Pattern), locatorpattern
Content

  • Scenario
  • Target
  • Solution
  • Implementation Details
  • Thoughts
  • Related Mode
  • More information
  • References
Download Demo scenario

You have a class that depends on several Service services (in fact, these services can be seen as another class). These services specify the specific type during the compilation phase. In the following example, ClassA depends on ServiceA and ServiceB in the compilation phase. This problem is explained.

This situation has the following Disadvantages:

  • If you replace or update the dependent services (or classes), you must modify the source code and recompile the solution;
  • The specific implementation of these dependencies (the specific implementation of The dependent service) must be available at compilation; the concrete implementation of The dependencies must be available at compile time.
  • It is difficult for your classes to be tested separately because they directly reference those dependencies. This means that these dependencies cannot be replaced with stubs or mock objects;
  • Your class contains repeated code for creating, searching, and managing these dependencies.

The following describes how to solve this problem.

Target

The server positioning mode can be used to accomplish any of the following goals:

  • You want to decouple your classes from those dependencies so that those dependencies can be replaced or updated without needing or rarely modifying your classes;
  • You want to write logic code, and the logic Code depends on the dependent classes, but the specific implementation of those dependent classes is unknown in the compilation phase;
  • You want to test your class independently without using those dependencies;
  • You do not want to write code for searching and managing dependent classes in your class;
  • You want to divide your applications into loosely coupled modules, so that you can develop, test, control, and deploy your applications separately.
Solution

Create a service locator that contains references of those services and encapsulates the logic for locating them. In your class, use the service locator to obtain the service instance. Describes how the class uses the server positioner.

The server locator mode does not describe how to instantiate a service. It describes a method for registering a service and locating a service. Generally, the service locator mode is combined with the Factory pattern and/or Dependency Injection pattern ). This combination allows the service locator to create a service instance.

Note:

A service locator can locate a service without knowing its specific type. For example, it may use a string key or service interface type ). In this way, you can replace the specific implementation of the dependency without modifying the class.

Realistic details

The SharePoint Guidance Library provides the implementation of a service locator.SharePointServiceLocatorClass allows you to access a single IServiceLocator instance and manage the instance. The default implementation of an interface of this class is ActivatingServiceLocator. This class can be used to create and locate services.

The Partner Portal application shows how to use a service locator to register and locate a service, such as a repository, logging service, and configuration management service ). For more information, see The SharePoint Service Locator.

Thoughts

Before using the service locator mode, consider the following:

  • There are more solution elements to manage.
  • You must write additional code that adds service references to the service locator before your objects can use it.
  • Your classes have a dependency on the service locator.
  • The source code is more complex and difficult to understand.
  • You can use configuration data to define run-time relationships.
  • You must provide implementations of the services. because the Service Locator pattern decouples service consumers from service providers, it might be necessary to provide additional logic. this logic ensures that the service providers are installed and registered before service consumers try to locate them.
Related Mode

The following mode is related to the service locator mode:

Dependency Injection ). This mode solves the same problem as the service locator mode, but uses different methods.

Inversion of Control ). Service positioner is a special case of control reversal. It reverses the traditional control flow of an application. It is a called object, rather than a caller who controls the processing.

More information

Refer:

  • Inversion of Control and the Dependency Injection pattern on Martin Fowler's Web site
  • Service Locator on MSDN

For more information about The Service Locator, see The SharePoint Service Locator.

References
  • Microsoft Developer Network-The Service Locator Pattern
  • The SharePoint Service Locator
  • Control reversal and dependency injection modes by Martin Fowler
  • Service Locator
  • Common Service Locator library
  • Service Locator is an Anti-Pattern by Mark seann

 

Download Demo

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.