Skip to content
Latchkey

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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →