javadependency-injectioninversion-of-control

What is a DI Container?


I am watching this course video about dependency injection and the instructor talked about di-container but did not explain in detail, now I read some articles and I want to confirm that now I am getting this right. Below is simple program and my question is,

Is Program class below is kind of simplest di-container? if not how would simple di-container would be like

interface  Implementable {
    void doSmth();
}

class A implements Implementable {

    @Override
    public void doSmth() {

    }
}

class B {

    private Implementable i;

    public B(Implementable implementable) {
        this.i= implementable;
    }

    public void doSmth(){
        i.doSmth();
    }
}

Is this class di-container ?

class Program {
    B b = new B(new A());
    b.doSmth();
}

Solution

  • According to Dependency Injection Principles, Practices and Patterns, a DI Container is:

    "a software library that provides DI functionality and allows automating many of the tasks involved in Object Composition, Interception, and Lifetime Management. DI Containers are also known as Inversion of Control (IoC) Containers." (§3.2.2)

    At the very least, a DI Container allows Auto-Wiring, which is:

    "the ability to automatically compose an object graph from maps between Abstractions and concrete types by making use of the types' metadata supplied by the compiler and [its runtime environment]." (§12.1.2)

    This typically means that a DI Container will analyze a type's constructor and will inject dependencies into it, without the need of having to specify each constructor argument manually.

    From that perspective, your Program class is not a DI Container, but your Program class acts as a Composer, which is part of the Composition Root. A Composition Root is:

    "a (preferably) unique location in an application where modules are composed together." (§4.1)

    The Composer is the part of the Composition Root that does the actual construction of the object graphs.

    "It's an important part of the Composition Root. The Composer is often a DI Container, but it can also be any method that constructs object graphs manually" (§8)

    In your specific Composition Root, instead of using a DI Container, you are practicing Pure DI, which is:

    "the practice of applying DI without a DI Container." (§1.1.1)

    In other words, Pure DI is the practice of composing object graphs by hand using the new keyword of your language, opposed to using a DI Container, exactly as your Program class demonstrates.

    how would simple di-container [look] like

    DI Container implementations are typically fairly complex and there are many ways to build them. Their essence, however, lies in the maps between abstractions and concrete types. Because of this, most DI Containers internally use a dictionary or hash map. This Stack Overflow answer shows a simplistic dictionary-based implementation (using C#) in just a few lines of code.