Dependency Injection

When Dependency Injection (English dependency, dependency ' and injection, injection '; abbreviation DI) is the object-oriented programming refers to a concept which regulates the dependencies of an object at runtime: Requires an object, for example, in its initialization to another object, this is deposited dependence in a central location - it is therefore not generated by the initialized object itself.

Word Meaning

The term Dependency Injection was introduced in 2004 by Martin Fowler to clarify the former term Inversion of Control: " Inversion of Control is too generic a term, and ran thus people find it confusing. . As a result with a lot of discussion with various [ Inversion of Control ] advocates we settled on the name Dependency Injection "(Martin Fowler: )


With dependency injection, it is possible - according to the Single Responsibility Principle - to transfer the responsibility for the construction of the dependency network between a program from the individual classes in a central component objects.

In a classically constructed OO system each object itself is responsible for its dependencies, ie, objects and resources needed to create and manage. For each object must bring some knowledge of its environment, which would not require it to fulfill its ultimate purpose normally.

Dependency Injection transfers the responsibility for the creation and linking of objects into a separate component, such as an externally configurable framework. Thus, the code of the object is independent of its environment. This avoids unnecessary dependencies when compiling and especially facilitates the creation of unit tests.

Disadvantage can, however, depending on the DI framework used impact that program logic must be stored in configuration files, which can reduce the clarity and make maintenance more difficult: the developer must now take into account for understanding the code, the configuration which is also some aids Code Analysis (eg IDE supported finding dependencies or refactoring ) escapes.


Martin Fowler describes three different ways to set required references, which he associates with the term Dependency Injection: Interface Injection, Setter Injection and Constructor Injection. (Section " Forms of Dependency Injection " in ) all described by him practice using this method calls in which the dependencies are not to be set return value, but parameter.

Interface Injection

The injectors' class module defines an interface that must be implemented by dependent classes to get asked the dependencies available at runtime.

Interface Injectable {    void inject (dependency dependency ); }   Dependent class implements Injectable {    private dependency dependency;      public void inject (dependency dependency ) {      this.abhängigkeit = dependency;    } }   class Injizierer {    void method ( ) {      Injectable injectable = ...;      Dependency dependency = ...;      injizierbares.injiziere ( dependency );    } } Setter Injection

The dependent class provides methods that are used to represent the dependencies are available.

Interface IAbhängiges {    void setAbhängigkeit (dependency dependency ); }   Dependent class implements IAbhängiges {    private dependency dependency;      public void setAbhängigkeit (dependency dependency ) {      this.abhängigkeit = dependency;    } }   class Injizierer {    void method ( ) {      IAbhängiges dependent = ...;      Dependency dependency = ...;      abhängiges.setAbhängigkeit ( dependency );    } } Constructor Injection

Dependencies on other classes are provided through constructors available. PicoContainer is one of the frameworks that implement this type.

Interface IAbhängiges {    public -dependent (dependency parameter); }   Dependent class implements IAbhängiges {    private dependency dependency;      public -dependent (dependency dependency ) {      this.abhängigkeit = dependency;    } }   class Injizierer {    void method ( ) {      Dependency dependency = ...;      IAbhängiges dependent = new dependent ( dependency );    } } Other implementation options

It is also possible to implement Dependency Injection in other ways, such as those used in some frameworks. For example, dependencies, for ways of programming, by reflection or by directly setting the reference to it in memory are set without method calls.

Dependency Injection is a concept that can be implemented similar to the design pattern abstract factory itself. However, there are various frameworks for various programming languages ​​and platforms for implementation provide the final solutions. They implement the pattern with partly comprehensive, further leading functionality, such as reading the configuration files and their examination for formal correctness. See the list of dependency injection frameworks.