Reputation: 22605
So..I've been digging into IoC container and service locator.
I thought an IoC container is an IoC container, not a service locator because
The way you use it. You pass a service locator to a class that needs dependencies and then retrieve a dependencies via the container. On the other hand, you inject dependencies to your class using IoC container.
IoC containers support auto-wiring, whereas service locators don't.
I always thought that IoC containers support auto-wiring and it has to support it to be called an IoC container, but I had a conversion with some people who told me that IoC container doesn't have to support auto-wring.
If that's true, what makes an IoC container an IoC container except the way you use it?
Upvotes: 3
Views: 349
Reputation: 233367
A DI Container is a library that you can use in various ways:
As soon as a DI Container supports Auto-Wiring, you can use it in both ways, but you should only use it as a Composition Engine.
Since Service Locator is an anti-pattern, the glorified dictionary has no value. As a bare minimum then, I'd say that a library must support Auto-Wiring in order to be a DI Container. However, that still doesn't make it a valuable component. To derive value from a DI Container, it must support sophisticated convention-based heuristics; if it doesn't, you're better off writing the composition by hand (AKA Poor Man's DI).
Upvotes: 5