Service Locator in C#

Even though Service locator is an anti-pattern. There are still many corps using this pattern within their infrastructure with a comment “it works!”.

Here is a simple implementation of Service Locator in C#.

You can enhance Service Locator with Lazy Initialization.

Well if it works out for you and want to keep things simple, go for it. In terms of testability, you can still inject mocks and fakes into service locator. One handicap of service locator is that it hides details from you.

Another major difference between Service locator and Dependency Injection Container is that, with Service Locator there is no Lifetime Management of dependencies.