What is the Service Locator anti-pattern?
Updated Apr 28, 2026
Short answer
Service Locator is an anti-pattern where a class explicitly requests its dependencies from a central registry (the container) rather than having them injected.
Deep explanation
It hides a class's dependencies. Instead of looking at the constructor to see what the class needs, developers must read the entire source code to find hidden locator.Resolve<T>() calls. It breaks Inversion of Control and makes unit testing difficult without a global state.
Real-world example
Passing the IServiceProvider directly into controllers or domain models instead of specific interfaces.
Common mistakes
- Using Service Locator to bypass constructor injection simply to reduce the number of constructor parameters.
Follow-up questions
- Are there any valid use cases for Service Locator?