你如何处理“深”的问题?与 IoC 和 DI 的依赖关系?
我是 IoC 新手,正在使用 Unity。假设您有一个包含“n”个项目的解决方案,并且您希望使用 Unity 来注册和解决依赖项。假设您的组合根位于项目a中。假设您的解决方案中有以下项目。
一个 乙 c d
可以说 a 取决于 b 中的某些内容,b 取决于 c 中的某些内容以及 < em>c 取决于 d 中的某些内容,
我已经看到如何使用构造函数注入来解析 a =>; b 依赖项,但我不知道如何在不访问项目 a< 中配置和创建的容器的情况下解决 b 对 c 的依赖关系/em>.
解决嵌套依赖的方法是什么? 是否有解决深度依赖关系的讨论/博客/示例?
I am new to IoC and I am playing with Unity. Let' say you have a solution with 'n' projects and you want to use Unity to register and resolve the dependencies. Lets say your composition root is in project a. Let's say you have the following projects in the solution.
a
b
c
d
Lets say a depends on something in b, b depends on something in c and c depends on something in d
I have seen how you can use constructor injection to resolve the a => b dependency but I am stuck on how b's dependency on c can be resolved without access to the container that was configured and created in project a.
What is the approach for resolving the nested dependencies?
Is there a discussion/blog/example addressing the resolution of deep dependencies?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的组合根应该创建并传递所有依赖项,包括嵌套依赖项,因此它需要对所有相关程序集的引用(除非您使用反射提供它们)。
例如,您通常在创建
A
之前创建B
的实例(提供其依赖项C
)。如果你'手动',它看起来像这样:只要您注册所有适当的类型,您的依赖注入框架就会为您解析它们。
有关该主题的精彩文章,请参阅 Miško Hevery 的“依赖注入神话:引用传递。”
Your composition root should create and deliver all your dependencies, including nested ones, so it needs references to all the relevant assemblies (unless you're supplying them using reflection).
For example, you normally create an instance of
B
(supplying its dependency,C
) before creating anA
. If you did it 'by hand', it would look like this:As long as you register all the appropriate types, your dependency injection framework will resolve them for you.
For a great article on the subject, see Miško Hevery's "Dependency Injection Myth: Reference Passing."