Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is MVC4 using the Service Locator Anti-Pattern?

After reading "Dependency Injection in .NET" by Mark Seemann I stay away from the Service Locator which is an anti-pattern.

Upon reading the release notes on MVC 4 I see:

Improved Inversion of Control (IoC) via DependencyResolver: Web API now uses the service locator pattern implemented by MVC’s dependency resolver to obtain instances for many different facilities.

Thus I'm left curious and confused why Microsoft would use a service locator in 2012.

like image 854
Tom Stickel Avatar asked Feb 23 '12 07:02

Tom Stickel


People also ask

Why Service Locator is anti pattern?

Service Locator is a dangerous pattern because it almost works. You can locate Dependencies from consuming classes, and you can replace those Dependencies with different implementations — even with Test Doubles from unit tests.

What's the difference between the dependency injection and Service Locator patterns?

The main difference is how the dependencies are located, in Service Locator, client code request the dependencies, in DI Container we use a container to create all of objects and it injects dependency as constructor parameters (or properties). Dependency Injection doesn't require the use of a DI Container though.

Why we need to use Dependency Injection in MVC?

The advantages of using Dependency Injection pattern and Inversion of Control are the following: Reduces class coupling. Increases code reusing. Improves code maintainability.

What is Java Service Locator?

Service Locator - Service Locator is a single point of contact to get services by JNDI lookup caching the services. Cache - Cache to store references of services to reuse them. Client - Client is the object that invokes the services via ServiceLocator.


1 Answers

That's an implementation detail that you shouldn't care about. The important thing is that now that the Web API uses the DependencyResolver to resolve dependencies for many different facilities, you will be able to use a real dependency injection whenever you want to plug into those facilities. So in your code you will be using a real dependency injection. If Microsoft didn't use the DependencyResolver then it would have been you that must have used it (as a service locator anti-pattern) in your code in order to resolve dependencies when you want to implement some custom functionality. This would have been bad for you. Now it's bad for Microsoft but you don't care about them.

Thus I'm left curious and confused why Microsoft would use a service locator in 2012.

Because designing a framework is not the same as designing an application using a framework. There are some different things to take into consideration when designing a reusable framework such as ASP.NET MVC rather than just what's written in the books. Some example is to design the framework in such a way that a person using this framework will be able to take advantage of the best practices written in the books in his code using this framework.

like image 116
Darin Dimitrov Avatar answered Sep 21 '22 02:09

Darin Dimitrov