What Is a Service Locator?
A service locator is a central object that holds references to services and hands them out when code requests one by type or name. Rather than receiving its dependencies, a component calls the locator to fetch them on demand. It is an alternative to dependency injection and is often considered an anti-pattern because it hides what a class actually needs.
Why it matters
Pulling dependencies from a locator obscures a component true requirements and complicates testing, since the hidden lookups must be stubbed. Many teams prefer explicit injection so dependencies appear in the signature.
Related guides
What Is Inversion of Control?Inversion of control is a design style where a framework or container, rather than your own code, decides whe…
What Is Dependency Inversion?Dependency inversion is the principle that high-level modules and low-level details should both depend on abs…
What Is the Repository Pattern?The repository pattern provides a collection-like interface for accessing domain objects, hiding the details…