concept Service Locator in category dependency injection

appears as: Service Locator, Service Locator, A Service Locator
Dependency Injection

This is an excerpt from Manning's book Dependency Injection.

You may think that, although we’ve exposed four myths about DI, we have yet to make a compelling case against any of them. That’s true. In a sense, this book is one big argument against these common misconceptions, so we’ll certainly return to these topics later. For example, in chapter 5, section 5.2 discusses why Service Locator is an anti-pattern.

A Service Locator supplies application components outside the Composition Root with access to an unbounded set of Volatile Dependencies.

As it’s most commonly implemented, the Service Locator is a Static Factory that can be configured with concrete services before the first consumer begins to use it. (But you’ll equally also find abstract Service Locators.) This could conceivably happen in the Composition Root. Depending on the particular implementation, the Service Locator can be configured with code by reading a configuration file or by using a combination thereof. The following listing shows the Service Locator anti-pattern in action.

Listing 5.5 Using the Service Locator anti-pattern
public class HomeController : Controller
{
    public HomeController() { }    #1  

    public ViewResult Index()
    {
        IProductService service =
            Locator.GetService<IProductService>();    #2  

        var products = service.GetFeaturedProducts();    #3  

        return this.View(products);
    }
}

#1   HomeController has a parameterless constructor.
#2   HomeController requests an IProductService instance from the static Locator class.
#3   Uses the requested IProductService, as usual

Instead of statically defining the list of required Dependencies, HomeController has a parameterless constructor, requesting its Dependencies later. This hides these Dependencies from HomeController’s consumers and makes HomeController harder to use and test. Figure 5.3 shows the interaction in listing 5.5, where you can see the relationship between the Service Locator and the ProductService implementation.

Figure 5.3 Interaction between HomeController and Service Locator

05-03.eps

Years ago, it was quite controversial to call Service Locator an anti-pattern. The controversy is over: Service Locator is an anti-pattern. But don’t be surprised to find code bases that have this anti-pattern sprinkled all over the place.

Our personal history with Service Locator

Service Locator and I (Mark) had an intense relationship for a couple of years before we parted ways. Although I can’t remember exactly when I first came across a well-known article that described Service Locator as a pattern,4  it provided me with a potential solution to a problem that I’d been pondering for some time: how to inject Dependencies. As described, the Service Locator pattern seemed like the answer to all my issues, and I quickly set forth to develop a reusable library based on the pattern, which I conveniently named “Service Locator.”

Dependency Injection in .NET

This is an excerpt from Manning's book Dependency Injection in .NET.

2 Mark Seemann, “Pattern Recognition: Abstract Factory or Service Locator?” http://blog.ploeh.dk/2010/11/01/PatternRecognitionAbstractFactoryOrServiceLocator.aspx

10 For more variations, see Mark Seemann, “Service Locator is an Anti-Pattern,” 2010, http://blog.ploeh.dk/2010/02/03/ServiceLocatorIsAnAntiPattern.aspx