Comparison
Ambient contextvsService locator
Ambient context
the code called a static Current property for the clock or the user, and nothing in the signature admitted it.
A dependency reached through static, implicit state — the current time, the current user, the current transaction — rather than passed in. It reads beautifully and is the same trade as a service locator with the indirection removed: hidden coupling, tests that must set and reset global state, and behaviour that depends on what ran before. The two cases people most regret are the clock and the current principal, both of which are trivially injectable.
Full entry →Service locator
the class asks a global object for what it needs mid-method, so its dependencies are invisible until it runs.
A registry that components query for their collaborators, rather than being handed them. It solves the same problem as injection and loses on the point that matters: a constructor tells you what a class needs, and a locator call buried in a method does not, so missing registrations become runtime failures and tests need the whole registry stood up. It survives in places where injection is impractical — plugin loading, some legacy frameworks — and the honest position is that it is a fallback, not an equal.
Full entry →