Arguments against Inversion of Control containers

There are certainly people who think that DI Containers add no benefit, and the question is valid. If you look at it purely from an object composition angle, the benefit of a container may seem negligible. Any third party can connect loosely coupled components.

However, once you move beyond toy scenarios you should realize that the third party that connects collaborators must take on more that the simple responsibility of composition. There may also be decommissioning concerns to prevent resource leaks. As the composer is the only party that knows whether a given instance was shared or private, it must also take on the role of doing lifetime management.

When you start combining various instance scopes, using a combination of shared and private services, and perhaps even scoping some services to a particular context (such as a web request), things become complex. It’s certainly possible to write all that code with poor man’s DI, but it doesn’t add any business value – it’s pure infrastructure.

Such infrastructure code constitutes a Generic Subdomain, so it’s very natural to create a reusable library to address such concerns. That’s exactly what a DI Container is.

BTW, most containers I know don’t use names to wire themselves – they use Auto-wiring, which combines the static information from Constructor Injection with the container’s configuration of mappings from interfaces to concrete classes. In short, containers natively understand those patterns.

A DI Container is not required for DI – it’s just damned helpful.


A more detailed treatment can be found in the article When to use a DI Container.

Leave a Comment