用依赖注入解决循环依赖 [英] resolving circular dependencies with dependency injection

查看:186
本文介绍了用依赖注入解决循环依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在各种网站上看过几篇文章,提出通过使用依赖注入来解决.NET程序集之间的循环依赖关系。这可能会解决构建错误,但它并没有真正解决循环依赖,是吗?对我来说,架构似乎仍然存在逻辑错误。我是疯狂还是别人同意1)这不是DI的恒星使用,2)不是解决循环依赖问题的适当方式?

I've seen several articles on various websites that propose resolving circular dependencies between .NET assemblies by using dependency injection. This may resolve the build errors but it's not really resolving the circular dependency, is it? To me, there seems to still be a logical error in the architecture. Am I crazy or do others agree 1) this is a less than stellar use of DI, and 2) not the appropriate way to solve circular dependency issues?

推荐答案

如果在两个对象之间有循环依赖关系,这意味着您需要一个第三个对象,两个对象将依赖这些对象,因此它们将不依赖于彼此。这是一个文章,是您的问题的确切解决方案:

If you have circular dependencies between two objects, it means you need a third object, on which the two objects will depend on, so they won't depend on each other. Here is an article that is the exact solution to your problem:

http://misko.hevery.com/2008/08/01/circular-dependency-in-constructors-and-dependency-injection/

这篇关于用依赖注入解决循环依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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