Glossary · Automation software engineering and architecture
Dependency injection
Also known as: DI
German: Abhängigkeitsinjektion
In software engineering, dependency injection is a design technique in which a component receives the objects or services it depends on from outside, for example through constructor parameters, properties or method arguments, instead of creating or locating them itself.
- Software engineering
In one sentence
Dependency injection supplies a component's dependencies from outside instead of letting the component create them itself.
Example
A conveyor function block receives an interface reference to its drive at initialization, so tests can pass a simulated drive and the machine passes the real one.
How it applies
- Engineering: Dependency injection decouples a component from concrete implementations of what it uses. In object-oriented IEC 61131-3 code, a function block can receive interface references to devices or services; in C#, Java or C++ applications, frameworks can manage the wiring.
- Testing: Injected dependencies can be replaced with mocks or simulations, which makes Component test runs possible without hardware.
- Documentation: Document which dependencies a component expects, through which interface and when they must be supplied. Configuration of the wiring, such as which drive is connected to which conveyor, belongs in commissioning and service documentation if it can be changed in the field.
Dependency injection vs. inversion of control
Inversion of control is the general principle that a framework or caller controls the flow and provides what a component needs. Dependency injection is one specific way to apply it to dependencies. Overused, dependency injection can make it hard to see which implementation actually runs, which complicates troubleshooting.