Total Pageviews

Tuesday, November 27, 2012

Service Locator Pattern --a specilised version of Inversion of control

http://www.martinfowler.com/articles/injection.html

Use the Service Locator pattern to achieve any of the following objectives:
  • You want to decouple your classes from their dependencies so that these dependencies can be replaced or updated with little or no change to the classes.
  • You want to write logic that depends on classes whose concrete implementation is not known at compile time.
  • You want to be able to test your classes in isolation, without the dependencies.
  • You do not want the logic that locates and manages the dependencies to be in your classes.
  • You want to divide your application into loosely coupled modules that can be independently developed, tested, versioned, and deployed.

Solution

Create a service locator that contains references to the services and that encapsulates the logic that locates them. In your classes, use the service locator to obtain service instances. The following diagram illustrates how classes use a service locator.

How classes use a service locator
Ff648968.3622491a-3b9f-4368-aee4-493f7fb95027(en-us,PandP.10).png
The Service Locator pattern does not describe how to instantiate the services. It describes a way to register services and locate them. Typically, the Service Locator pattern is combined with the Factory pattern and/or the Dependency Injection pattern. This combination allows a service locator to create instances of services.

Example;
In Business we used the wcf.Unity and wcf service host factory

No comments:

Post a Comment