什么是 DI 容器?
What is a DI Container?
我正在看这个关于依赖注入的课程视频,讲师谈到di-container
但没有详细解释,现在我看了一些文章,我想确认一下我现在做对了。下面是简单的程序,我的问题是,
下面的程序class是不是最简单的二容器?如果不是,简单的双容器会是什么样子
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();
}
}
这是class双容器吗?
class Program {
B b = new B(new A());
b.doSmth();
}
根据Dependency Injection Principles, Practices, and Patterns,一个 DI 容器是:
"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)
最起码,一个DI Container是允许Auto-Wiring的,那就是:
"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)
这通常意味着 DI 容器将分析类型的构造函数并将依赖项注入其中,而无需手动指定每个构造函数参数。
从这个角度来看,您的 Program
class 不是 DI 容器,但您的 Program
class 充当Composer,它是 Composition Root 的一部分。一个 Composition Root 是:
"a (preferably) unique location in an application where modules are composed together." (§4.1)
Composer 是 Composition Root 的一部分,负责实际构造对象图。
"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)
在您特定的 Composition Root 中,您不是在使用 DI 容器,而是在练习 Pure DI,即:
"the practice of applying DI without a DI Container." (§1.1.1)
换句话说,Pure DI 是使用您的语言的 new
关键字手动组合对象图的实践,而不是使用 DI 容器,就像您的 Program
class 演示。
how would simple di-container [look] like
DI 容器实现通常相当复杂,构建它们的方法有很多种。然而,它们的本质在于抽象和具体类型之间的映射。因此,大多数 DI 容器在内部使用字典或哈希映射。然而,This Stack Overflow answer 仅用几行代码就显示了基于字典的简单实现(使用 C#)。
我正在看这个关于依赖注入的课程视频,讲师谈到di-container
但没有详细解释,现在我看了一些文章,我想确认一下我现在做对了。下面是简单的程序,我的问题是,
下面的程序class是不是最简单的二容器?如果不是,简单的双容器会是什么样子
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();
}
}
这是class双容器吗?
class Program {
B b = new B(new A());
b.doSmth();
}
根据Dependency Injection Principles, Practices, and Patterns,一个 DI 容器是:
"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)
最起码,一个DI Container是允许Auto-Wiring的,那就是:
"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)
这通常意味着 DI 容器将分析类型的构造函数并将依赖项注入其中,而无需手动指定每个构造函数参数。
从这个角度来看,您的 Program
class 不是 DI 容器,但您的 Program
class 充当Composer,它是 Composition Root 的一部分。一个 Composition Root 是:
"a (preferably) unique location in an application where modules are composed together." (§4.1)
Composer 是 Composition Root 的一部分,负责实际构造对象图。
"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)
在您特定的 Composition Root 中,您不是在使用 DI 容器,而是在练习 Pure DI,即:
"the practice of applying DI without a DI Container." (§1.1.1)
换句话说,Pure DI 是使用您的语言的 new
关键字手动组合对象图的实践,而不是使用 DI 容器,就像您的 Program
class 演示。
how would simple di-container [look] like
DI 容器实现通常相当复杂,构建它们的方法有很多种。然而,它们的本质在于抽象和具体类型之间的映射。因此,大多数 DI 容器在内部使用字典或哈希映射。然而,This Stack Overflow answer 仅用几行代码就显示了基于字典的简单实现(使用 C#)。