程序集 A1 中的类 C 实现程序集 A2 中的接口。使用C时,需要同时引用A1和A2
这是设计使然,根据这个SO问题<中的答案/a>
但我真的不想引用这两个程序集,感觉就像我破坏了封装(A1 的用户不应该知道 A1 使用 A2 来实现......)
我想移动接口定义到另一个程序集,让我们称之为“Core”,并让 A2 和 A1 的客户引用它。这对我来说感觉更干净。
我的问题是:这样的设计好吗?或者是否有一个我不知道的标准 .NET 解决方案?另一方面,我担心“核心”最终会成为完全不相关的接口的令人讨厌的组合......你觉得怎么样?
This is by design, according to the answers in this SO question
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...)
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,这不会破坏封装。
封装意味着实现细节是不相关的——只要您针对接口进行编程,封装就完好无损。
其次 - 如果您想同时使用接口和实现,则需要引用两者所在的程序集(不考虑插件架构)。否则你的代码会如何工作?如果没有对实现的引用,则无法实例化具体类型。如果没有对该接口的引用,则无法在代码中使用它。
First of all, this doesn't break encapsulation.
Encapsulation means that the implementation details are not relevant - so long as you program to the interface, encapsulation is intact.
Secondly - if you want to use both the interface and implementation, you need to reference the assemblies both are in (disregarding plug-in architectures). How else would your code work? If there is no reference to the implementation, you can't instantiate a concrete type. If there is no reference to the interface, you can't use it in your code.
C# 没有私有继承。如果类 A1 实现接口 A2,那么这就是公共信息,该信息暴露给类 A1 的用户,就像 A2 是基类而不是接口一样。
是的,您必须引用这两个程序集。
C# does not have private inheritance. If class A1 implements interface A2, then that is public information, which is exposed to users of class A1, exactly as though A2 were a base class and not an interface.
Yes, you must reference both assemblies.
我满意地解决了这个问题,请告诉我你的想法:(
警告:这是特定于 IDE 的,不知道我是否可以在 SharpDevelop 中实现)
在 A1 中,而不是引用 A2程序集,在 Visual Studio 中,右键单击 A1 项目并选择“添加现有项”。选择接口文件。 在按“添加”按钮之前,请注意它是一个下拉按钮:将其放下并选择“添加为链接”。如果不这样做,就必须维护界面的多个副本,这很麻烦
删除从 A1 到 A2 的引用(如果你使用的只是接口)
瞧! A1 的客户只需要对 A1 的引用。我认为这是因为现在接口是在 A1 和 A2 程序集中定义的类型。我不认为这会危及维护,因为接口仍然只在一个源代码文件中定义。好吧,它被编译成两个程序集,所以它是一种“二进制重复”,但我不认为这很糟糕......你觉得吗?
I solved it to my satisfaction, please tell me what you think:
(Warning: this is IDE-specific, don't know if I could pull it off in, say, SharpDevelop)
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
Remove the reference from A1 to A2 (if all you used from it was the interface)
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?