Skip to content

Dependency injection

A component receives dependencies from outside instead of creating concrete implementations itself.

Also known asDI
Software architecture

What is dependency injection?

With dependency injection, a component receives required dependencies from outside, for example as constructor parameters. It does not have to create the concrete implementation itself. Assembling the parts is separated from using them. Ordinary program code can do this; the principle does not require a dedicated DI container.

In a fictional example, a report component is given a data reader. In operation it reads the intended source; for a focused test, a suitable test implementation can supply fixed data. This supports isolated checking but does not demonstrate correct connection to the real source. Lifetime, configuration and release of required resources still need decisions. DI is a specific form of inversion of control; not every inversion of control or service locator is DI.

Sources