问题描述
这是设计使然,根据 this所以问题
但我真的不想引用这两个程序集,感觉就像我破坏了封装(A1 的用户不应该知道 A1 使用 A2 来实现......)
But I really don't want to reference both assemblies, it feels like I'm breaking encapsulation (the users of A1 shouldn't know that A1 uses A2 for its implementation...)
我想把接口定义移到另一个程序集,我们称之为核心",让 A2 和 A1 的客户端都引用它.这让我感觉更干净.
I thought of moving the interface definition to another assembly, let's call it "Core", and have both A2 and A1's client reference it. This feels cleaner to me.
我的问题是:这是好的设计吗?或者是否有我不知道的标准 .NET 解决方案?另一方面,我担心核心"最终会成为完全不相关的接口的令人讨厌的组合......你怎么看?
My question is: Is this good design? Or is there a standard .NET solution to this that I'm not aware of? On the other hand, I worry that "Core" will end up being a nasty mix of totally unrelated interfaces... What do you think?
推荐答案
我已经解决了,我很满意,请告诉我你的想法:
I solved it to my satisfaction, please tell me what you think:
(警告:这是特定于 IDE 的,不知道我是否可以将其实现,例如 SharpDevelop)
(Warning: this is IDE-specific, don't know if I could pull it off in, say, SharpDevelop)
在 A1 中,而不是引用 A2 程序集,在 Visual Studio 中,右键单击 A1 项目并选择添加现有项目".选择接口文件.在按下添加按钮之前,请注意它是一个下拉按钮:将其放下并选择添加为链接".如果你不这样做,你将不得不维护界面的多个副本,这很麻烦
In A1, instead of referencing the A2 assembly, in Visual Studio, right-click the A1 project and select "Add Existing Item". Select the interface file. Before pressing the Add Button, notice it's a dropdown button: drop it and select "Add as Link". If you don't do it this way, you'll have to mantain multiple copies of the interface, which is a hassle
删除从 A1 到 A2 的引用(如果你使用的只是接口)
Remove the reference from A1 to A2 (if all you used from it was the interface)
瞧!A1 的客户只需要对 A1 的引用.我认为这是因为现在接口是在 A1 和 A2 程序集中定义的类型.我不认为这会危及维护,因为接口仍然只在一个源代码文件中定义.好吧,它被编译成两个程序集,所以它是一种二进制重复",但我不认为这很糟糕......你呢?
Voila! A1's clients only need a reference to A1. I think this is because now the interface is a type which is defined in both the A1 and A2 assemblies. I don't think that endangers manteinance, because the interface is still defined in only one source code file. Ok, its's compiled into two assemblies, so it's a sort of "binary duplication", but I don't think that's bad... Do you?
这篇关于程序集 A1 中的类 C 实现了程序集 A2 中的接口.使用C时,需要同时引用A1和A2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!