DI 容器如何工作的最简单解释?
简单来说和/或在高级伪代码中,DI 容器如何工作以及如何使用?
In simple terms and/or in high-level pseudo-code, how does a DI container work and how is it used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
DI 容器的核心是根据接口和具体类型之间的映射创建对象。
这将允许您从容器请求抽象类型:
这要求您之前配置容器以从 IFoo 映射到实现 IFoo 的具体类(例如 Foo)。
这本身并不会特别令人印象深刻,但 DI 容器做得更多:
一旦您开始尝试手动管理组合和生命周期,您应该开始欣赏 DI 容器提供的服务:)
许多 DI 容器可以做的远不止上述,但是那些是核心服务。大多数容器都提供通过代码或 XML 进行配置的选项< /a>.
当谈到容器的正确使用时,Krzysztof Kozmic 刚刚发布了很好的概述。
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:
This requires that you have previously configured the container to map from IFoo to a concrete class that implements IFoo (for example Foo).
This in itself would not be particularly impressive, but DI Containers do more:
Once you start trying to manually manage composition and lifetimes you should start appreciating the services provided by a DI Container :)
Many DI Containers can do much more than the above, but those are the core services. Most containers offer options for configuring via either code or XML.
When it comes to proper usage of containers, Krzysztof Kozmic just published a good overview.
虽然上面的说法是一种轻描淡写的说法,但这是考虑它们的简单方法。给定集合,如果您请求类的相同实例 - DI 容器将决定是否为您提供一个 它们
的使用使连接依赖项变得更加容易和清晰。
想象一下,在没有 DI 容器的情况下构建房屋是一件痛苦的事情, 一个卧室的实例,然后是一个厨房的实例,如果这些对象也有依赖关系,那么您需要将它们连接起来,然后您才能创建一个对象。使用 DI/IOC(控制反转)容器,您说您想要一个房屋对象,DI 容器将递归创建其每个依赖项并返回一个房屋。
没有 DI/IOC 容器:
使用 DI/IOC 容器:
最终,它们使代码更易于理解、更易于编写,并将将对象连接在一起的责任从手头的问题转移开。
While the above is a massive understatement that's the easy way of thinking about them. Given the collection, if you ask for the same instance of an class - the DI container will decide whether to give you a cached version or a new one, or so on.
Their usage makes it easier and cleaner when it comes to wiring up dependencies. Imagine you have the following pseudo classes.
Constructing a house is a pain without a DI container, you would need to create an instance of a bedroom, followed by an instance of a kitchen. If those objects had dependencies too, you would need to wire them up. In turn, you can spend many lines of code just wiring up objects. Only then could you create a valid house. Using a DI/IOC (Inversion of Control) container you say you want a house object, the DI container will recursively create each of its dependencies and return you a house.
Without DI/IOC Container:
With DI/IOC Container:
At the end of the day they make code easy to follow, easier to write and shift the responsibility of wiring objects together away from the problem at hand.
您配置 DI 容器,以便它了解您的接口和类型 - 每个接口如何映射到类型。
当您对其调用
Resolve
时,它会查看映射并返回您请求的映射对象。某些 DI 容器使用约定优于配置,例如,如果您定义接口
ISomething
,它将查找具体的Something
类型来实例化并返回。You configure a DI container so it knows about your interfaces and types - how each interface maps to a type.
When you call
Resolve
on it, it looks at the mapping and returns the mapped object you have requested.Some DI containers use conventions over configuration, where for example, if you define an interface
ISomething
, it will look for a concreteSomething
type to instantiate and return.