39

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();
}
Steven
  • 166,672
  • 24
  • 332
  • 435
Abdurakhmon
  • 2,813
  • 5
  • 20
  • 41
  • quick search yielded https://www.dotnettricks.com/learn/dependencyinjection/what-is-ioc-container-or-di-container – Nkosi Jun 06 '18 at 10:59
  • 1
    You are confusing terms. the `Program` class in this context is the composition root and is doing pure DI. – Nkosi Jun 06 '18 at 11:00
  • 1
    Another very helpful article on the subject http://blog.ploeh.dk/2014/06/10/pure-di/ – Nkosi Jun 06 '18 at 11:02
  • According to this article http://fabien.potencier.org/do-you-need-a-dependency-injection-container.html di-container instantiates and configures objects, is'nt that what `Program` class is doing – Abdurakhmon Jun 06 '18 at 11:03

1 Answers1

51

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, however, shows a simplistic dictionary-based implementation (using C#) in just a few lines of code.

mamen
  • 1,202
  • 6
  • 26
Steven
  • 166,672
  • 24
  • 332
  • 435
  • 3
    There is some free content available for the book referenced at the start. As someone trying to understand DI, it was quite helpful: https://freecontent.manning.com/dependency-injection-writing-maintainable-loosely-coupled-code/ – Brian Jan 15 '20 at 16:54
  • Also, chapter 1 of the book can be read in its entirety online, freely. Just go to the book's home page and click on chapter 1 in the table of content. – Steven Feb 16 '20 at 11:09