Dependency Injection: Introduction
Table of Contents
Dependency Injection
Dependency injection is a technique where dependencies are injected into the code from the outside. This means that a class that uses another class internally does not use the concrete class but uses an interface or an abstract class. The interface or the abstract class is described from the outside via method, property, or constructor with the concrete class, thereby the dependence is injected from the outside. It sounds more complicated than it actually is. For a better understanding, here is a small example. In the example it is to be recognized very well, how the dependencies are injected from the outside into. Furthermore, it is to be recognized very well how simply individual components of the software are to be exchanged because simply new classes with the appropriate interfaces must be implemented and replaced at the declaration. The classes themselves have no dependencies to concrete implementations of other classes, and exactly that is the goal of the technique.
The Example
For the example, we take a simple stopper which is used to separate carriers.
The stopper unit has:
1 cylinder without inputs and with one output
1 sensor detects if a carrier is at the stopper
1 jam sensor that detects if the section of the stopper is full
1 interface to the conveyor with:
startRequest
from stopper to conveyorconveyorIsRunning
from conveyor to stopper
2 interfaces to the previous and next section
readyToReceive
from receiver to senderpartReceived
from receiver to sender
The example is simplified now here strongly since it concerns only dependency injection, in the example error handling, mode of operation handling etc… are omitted.