DEV Community

Alex Georghiou
Alex Georghiou

Posted on

Dependency Injection Does Not Fix Bad Dependencies

Dependency Injection Does Not Fix Bad Dependencies

Dependency injection makes dependencies visible. It does not make them sensible.

Take a service that needs a repository, a notification sender, three validators, a configuration provider, two mappers, a logger, and a factory. You can register all ten in a container and the application will start. The constructor is still trying to tell you something: this class probably owns too many decisions.

A common response is to hide that list behind a facade. That shortens the constructor and leaves the design problem intact. Worse, the facade often becomes a service locator with a respectable name.

I prefer to ask what changes together. Validation rules may belong to one policy object. Sending a notification may be an outcome of a completed operation rather than a synchronous dependency. Mapping might not need its own service at all. The goal is to reduce reasons for change, not merely parameters.

Lifetimes are the other half of this story. Injecting a short-lived object into a long-lived one can preserve stale state or consume a disposed resource. A container can validate some graphs, but the developer still has to understand state ownership.

One practical review: draw the object graph around a request handler. Circle the objects touching I/O. Underline the objects holding mutable state. Any object that does both deserves scrutiny.

Use DI to enforce boundaries you already understand. Do not expect a registration framework to discover those boundaries for you.

Top comments (0)