什么是DI容器? [英] What is a DI Container?

查看:109
本文介绍了什么是DI容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看有关依赖注入的本课程视频,并且讲师讨论了 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,

下面的Program类是最简单的di容器吗?如果不是这样的话,简单的di-container会是什么样子

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 Program {
    B b = new B(new A());
    b.doSmth();
}

推荐答案

根据依赖注入原理,实践和模式,DI容器是:

"一个软件库,该软件库提供DI功能并允许自动执行对象组成拦截生命周期管理.DI容器也称为控制反转(IoC)容器."(第3.2.2节)

"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容器允许自动装配,即:

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

"能够利用编译器及其[运行时环境]提供的类型元数据从抽象与具体类型之间的映射中自动组成对象图.em>"(第12.1.2节)

"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容器将分析类型的构造函数并将依赖项注入其中,而无需手动指定每个构造函数参数.

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.

从这个角度来看,您的 Program 不是 DI容器,但是您的 Program 类充当Composer,是Composer的一部分组成根.组成根是:

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:

"在模块组成在一起的应用程序中的一个(最好是)唯一位置."(第4.1节)

Composer是合成根"的一部分,它负责实际构造对象图.

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

"它是组成根的重要组成部分. Composer 通常是 DI容器,但也可以是任何手动构造对象图的方法(§8)

"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)

在特定的合成根目录中,您正在练习纯DI ,即:

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

"在没有DI容器的情况下应用DI的实践."($ 1.1.1)

换句话说,Pure DI是一种使用语言的 new 关键字手动组成对象图的做法,而不是像您的 Program 那样使用DI容器>课堂演示.

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.

简单的di容器[外观]会怎么样

how would simple di-container [look] like

DI容器的实现通常相当复杂,并且有很多构建它们的方法.但是,它们的本质在于抽象和具体类型之间的映射.因此,大多数DI容器在内部都使用字典或哈希图.此堆栈溢出答案仅用几行代码就显示了基于字典的简化实现(使用C#).

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.

这篇关于什么是DI容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆