When would you use the Common Service Locator?

Imagine you are writing library code to be used by 3rd party developers. Your code needs to be able to create service objects that these developers provide. However you don’t know which IoC container each of your callers will be using. The Common Service Locator lets you cope with the above without forcing a given … Read more

Which pattern to use for logging? Dependency Injection or Service Locator?

I personally do a mixture of both. Here are my conventions: From a static context – Service Location From an instance context – Dependency Injection I feel this gives me the right balance of testability. I find it a little harder to setup tests against classes that use Service Location than use DI, so this … Read more

Is the Service Locator pattern any different from the Abstract Factory pattern?

I have stumbled across the same question while investigating these patterns. I think the major differences can be found between a Service Locator and a Factory (whether it is abstract or not): Service Locator ‘Locates’ an existing dependency (the service). Although the service may be created during resolution, it is of no consequence to the … Read more

Dependency Injection vs Service Location

Because the class is now being injected with an IoC container, then why not use it to resolve all other dependencies too? Using the service locator pattern completely defeats one of the main points of dependency injection. The point of dependency injection is to make dependencies explicit. Once you hide those dependencies by not making … Read more

Why is MVC4 using the Service Locator Anti-Pattern?

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 … Read more

Accessing ASP.NET Core DI Container From Static Factory Class

You can avoid the static classes and use Dependency Injection all the way through combined with: The use of IApplicationLifetime to start/stop the listener whenever the application starts/stops. The use of IServiceProvider to create instances of the message processors. First thing, let’s move the configuration to its own class that can be populated from the … Read more

Is ServiceLocator an anti-pattern?

If you define patterns as anti-patterns just because there are some situations where it does not fit, then YES it’s an anti pattern. But with that reasoning all patterns would also be anti patterns. Instead we have to look if there are valid usages of the patterns, and for Service Locator there are several use … Read more

What’s the difference between the Dependency Injection and Service Locator patterns?

The difference may seem slight, but even with the ServiceLocator, the class is still responsible for creating its dependencies. It just uses the service locator to do it. With DI, the class is given its dependencies. It neither knows, nor cares where they come from. One important result of this is that the DI example … Read more