Question

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

  1. 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.

  2. 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?

Was it helpful?

Solution

A DI Container is a library that you can use in various ways:

  • The simplest (pseudo) DI Container you can write is basically juts a glorified dictionary. You can build one in 15 lines of code. However, it doesn't do Auto-Wiring, which means that it doesn't do composition, so at this point you can only use it as a Service Locator, but since Service Locator is an anti-pattern, there's not much benefit from that.
  • A better DI Container also does Auto-Wiring. As soon as it can do Auto-Wiring, you can also use it as a Composition Engine. This means that you can design your own code exclusively on container-agnostic patterns like Constructor Injection, and then in the Composition Root ask the Composition Engine (DI Container) to compose everything together.

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).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top