Understanding the Service Locator pattern
Service Locator is an anti-pattern that reverts the IoC principle to its Control Freak roots. The only difference is the use of the IoC container to build the dependency tree instead of the new
keyword.
There is some use of this pattern in ASP.NET, and some may argue that there are some reasons for one to use the Service Locator pattern, but it should happen very rarely or never. For that reason, in this book, let's call Service Locator a code smell instead of an anti-pattern.
The Service Locator pattern is used internally by the DI container to find dependencies, which is the correct way of using it. In your applications, you want to avoid injecting an IServiceProvider
to get the dependencies you need from it, which revert to the classic flow of control.
My strong recommendation is don't use Service Locator unless you know what you are doing and have no other option.
A good use of Service Locator could be to migrate...