什么是依赖注入上下文中的组合根? [英] What is a composition root in the context of dependency injection?

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

问题描述

我正在探索依赖注入,并且到处都在使用术语组合根.那是什么?

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

推荐答案

组合根是应用程序中使用依赖注入容器(尽管这是done 无关紧要,可以使用容器,也可以使用 pure 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
  • 在 WCF 中,这将在 ServiceHostFactory 中

有一个很好的答案这里 更详细地解释了这一点.

There is a good answer here which explains a bit more about this.

另见这个答案.

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

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