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(); }
A DI Container is a framework to create dependencies and inject them automatically when required. It automatically creates objects based on the request and injects them when required. DI Container helps us to manage dependencies within the application in a simple and easy way.
At its core a DI Container creates objects based on mappings between interfaces and concrete types. This will allow you to request an abstract type from the container: IFoo f = container.
IoC Container (a.k.a. DI Container) is a framework for implementing automatic dependency injection. It manages object creation and it's life-time, and also injects dependencies to the class.
Spring IoC (Inversion of Control) Container is the core of Spring Framework. It creates the objects, configures and assembles their dependencies, manages their entire life cycle. The Container uses Dependency Injection(DI) to manage the components that make up the application.
According to Dependency Injection Principles, Practices, and Patterns, a DI Container is:
"a software library that 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With