All glossary terms
Design

Dependency injection

Dependency injection is the design pattern where a component receives its dependencies as parameters rather than constructing them internally. The pattern decouples a component from the concrete implementations of its dependencies, making the component easier to test (inject mocks) and easier to reconfigure (inject different implementations).

DI is one face of inversion of control: instead of the component controlling which database client it uses, the caller (or a framework) decides and passes it in. The pattern's value scales with the testability and configurability needs of the component. In small codebases, DI can feel like over-engineering — passing five dependencies into a constructor that could just import them. In large codebases, DI is essential because the alternative (hardcoded imports) makes testing impossible and migration painful. Framework support (Spring, Angular, NestJS, .NET DI container) handles the wiring; manual DI (constructor params) is also fine for smaller surfaces.

Related terms