在依赖注入的上下文中的组合根是什么? [英] What is a composition root in the context of Dependency Injection
问题描述
我正在探索依赖注入,术语强组合根被用于整个地方。那么它是什么?
I am exploring dependency injection and the term composition root is used all over the place. So what is it?
推荐答案
组合根是应用程序中单个位置,用于应用程序的对象图形发生,使用依赖注入容器(尽管这样做是无关紧要的,它可能是使用容器或者可以使用纯DI )。
The composition root is the single place in your application where the composition of the object graphs for your application take place, using the dependency injection container (although how this is done is irrelevant, it could be using a container or could be done manually using pure DI).
只有一个地方发生这种情况,您的容器不需要在组合根。
There should only be one place where this happens and your container should not need to be used outside of the composition root.
从下面链接的一个答案中引用:
Quoting from one of the answers linked to below:
实际上,这意味着你
应该在应用程序的
根目录下配置容器。
In practice, this means that you should configure the container at the root of your application.
- 桌面应用程序,将在Main方法(或非常接近它)
- 在ASP.NET(包括MVC)应用程序中,将在
Global.asax - 在W中CF,将在一个ServiceHostFactory
- 等。
- In a desktop app, that would be in the Main method (or very close to it)
- In an ASP.NET (including MVC) application, that would be in Global.asax
- In WCF, that would be in a ServiceHostFactory
- etc.
有一个很好的答案这里这更多地解释了这一点。
There is a good answer here which explains a bit more about this.
另请参见这个答案。
这篇关于在依赖注入的上下文中的组合根是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!