如何解决两个紧密耦合的命名空间(在不同的程序集中)之间的依赖关系? [英] how to resolve a dependency between two tightly coupled namespace(in different assembly)?
问题描述
假设我有一个名为ITestService
的接口,它具有成对的实现,在程序集"A"中将其称为DefaultTestService
.我在Autofac容器中将DefaultTestService
类注册为ITestService
服务.另一方面,程序集"A"具有对程序集"B"的引用.现在,我要通过程序集"B"中对应的接口(ITestService)解析DefaultTestService,为此,我必须先添加对程序集"A"的引用这样我就可以访问ITestService并将其传递给Resolve<T>()
方法.
suppose i have an interface called ITestService
with it's paired implementation, lets call it DefaultTestService
in Assembly "A". I register DefaultTestService
class as ITestService
service in Autofac container. On the other hand assembly "A" has a reference to assembly "B".now I want to resolve DefaultTestService through it's corresponding interface (ITestService) in assembly "B", for doing this first I have to add reference to assembly "A" so that I can access and pass ITestService to Resolve<T>()
method.
循环依赖!
好的.我们可以通过为一个组件分配一个键来解决它,并通过键来解决它.但是如果我没有注册依赖项(也没有分配给它一个键)怎么办?
ok.we can solve it by assigning a key for a component and resolve it by key.but what if the dependency not registered by me (and a key not assigned to it)?
有什么主意吗? 谢谢你的建议.
Any idea? thanks for you'r suggestion.
推荐答案
如果程序集A依赖于程序集B,并且两个程序集都需要ITestService
,则必须在程序集B中定义此接口,或者在程序集B中定义该接口,而程序集A和B都依赖于此程序集上.
If assembly A depends on B and both assemblies need ITestService
, this interface must be defined in assembly B, or a third assembly that both A and B depend on.
这篇关于如何解决两个紧密耦合的命名空间(在不同的程序集中)之间的依赖关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!