RSS

Service Locator Pattern/Anti-Pattern?

01 Jun

I was thinking of writing some code on Service Locator pattern until recently I notice loads of resources on the internet state that it is an anti-pattern. Service Locator seems appealing when I first saw it implemented with Unity DI container in Project Silk, a Patterns & Practices article by Microsoft. But the idea of having an global variable like object resolving dependencies rather than just using a DI container bothers me a lot. After reading these arguments, I decided to avoid using Service Locator pattern in MVC.

Here is a thorough introduction of Service Locator and DI by Martin Fowler :
Inversion of Control Containers and the Dependency Injection pattern

These articles point out the cons of Service Locator:
Is IDependencyResolver an anti-pattern?
IoC.Resolve vs Constructor Injection
Service Locator is an Anti-Pattern by Mark Seemann

The purpose of IDependencyResolver in MVC 3 and 4 seems to be supporting legacy codes:
Why is MVC4 using the Service Locator Anti-Pattern
ASP.NET MVC 3 Service Location, Part 5: IDependencyResolver

References:

ASP.NET MVC: Resolve or Inject? That’s the Issue…

Dependency Inject (DI) “friendly” library

Defense by Service Locator pattern side, but the definition of Service Locator here became the major point of the discussion:
To IServiceLocator or not

Advertisements
 
Leave a comment

Posted by on June 1, 2012 in .NET, ASP.NET, Design Pattern, MVC3

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: