依赖注射容器?它有什么作用? [英] Dependency injection container? What does it do?

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

问题描述

我一直在DI上阅读,看起来像一个简单的概念。我没有得到的是容器。假设我想要创建自己的容器。使用像detect这样的动词,我不知道容器如何检测到一个新的依赖对象被创建并且知道它是依赖关系的。对我来说,这个容器似乎是一个荣耀的工厂。

I have been reading up on DI and it seems like a simple enough concept. What I don't get is the container. Let’s say for a moment that I want to create my own container. Verbs like "detect" are used and I don't get how the container "detects" that a new dependent object was created and know to inject it's dependencies. To me it seems like the container is a glorified factory.

任何人都可以解释一个容器是如何实际实现的,还是指向一个资源?

Can any one explain how a container is actually implemented, or maybe point me to a resource?

谢谢!

推荐答案

这是从 Windsor文档


控制反转

控制反转是框架使用的一种原则,以
的方式允许开发人员扩展框架或使用
它。基本思想是框架知道程序员的
对象,并对它们进行调用。

Inversion of Control is a principle used by frameworks as a way to allow developers to extend the framework or create applications using it. The basic idea is that the framework is aware of the programmer's objects and makes invocations on them.

这与使用API​​相反,代码使
调用API代码。因此,框架反转控件:
它不是开发人员代码,而是框架
基于一些刺激的呼叫。

This is the opposite of using an API, where the developer's code makes the invocations to the API code. Hence, frameworks invert the control: it is not the developer code that is in charge, instead the framework makes the calls based on some stimulus.

你可能已经在这样的情况下开发了这个原则,但即使你没有意识到这一点。

You have probably been in situations where you have developed under the light of this principle, even though you were not aware of it.

控制容器的反转

控制容器的反转使用上述原理
(简而言之)管理类。也就是说,它们的创建,破坏,
的生命周期,配置和依赖关系。这样,类不要
需要获取和配置它们依赖的类。这个
显着减少了系统中的耦合,因此
简化了重用和可测试性。

An Inversion of Control Container uses the principle stated above to (in a nutshell) manage classes. That is, their creation, destruction, lifetime, configuration, and dependencies. This way classes do not need to obtain and configure the classes they depend on. This dramatically reduces coupling in a system and, as a consequence, simplifies reuse and testability.

有人认为有些混乱Control的反转
是控制容器的反转的同义词。如
所示,反转控制是一个更广泛的原则。

There is some confusion created by people that think that 'Inversion of Control' is a synonym for 'Inversion of Control Container'. As stated, Inversion of control is a broader principle.

通常人们认为这是关于注入,并广播
这个是IoC容器的主要目的。实际上,
注入是一种解耦方式,而不是主要的
目的。

Often people think that it is all about "injection", and broadcast that this is the primary purpose of IoC containers. In fact, "injection" is a consequence, a means to decouple, not the primary purpose.

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

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