在依赖注入的背景下,什么是复合根? [英] What is a composition root in the context of dependency injection?

查看:77
本文介绍了在依赖注入的背景下,什么是复合根?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究依赖项注入,并且整个地方都使用术语 composition root 。那是什么呢?

解决方案

组合根是应用程序中单个位置,其中应用程序的对象图构成使用依赖项注入容器进行操作(尽管无关紧要,但是可以使用容器,也可以使用应该只有一个地方发生这种情况,并且您的容器不需要



引用以下链接之一的答案:


实际上,这意味着您
应该在应用程序的
根目录下配置容器。




  • 在桌面应用程序中,它将位于Main方法中(或非常接近)

  • 在ASP.NET(包括MVC)应用程序中,它将位于$ b中$ b Global.asax

  • 在WCF中,它将在ServiceHostFactory中

  • 等。


有一个很好的答案这里对此进行了更多解释。



另请参见此答案


I am exploring dependency injection and the term composition root is used all over the place. So what is it?

解决方案

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.

  • 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.

See also this answer.

这篇关于在依赖注入的背景下,什么是复合根?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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