从组件A1 C级实现了从组件A2接口。当使用C,则需要同时引用A1和A2 [英] Class C from assembly A1 implements interface from assembly A2. When using C, need to reference both A1 and A2

查看:185
本文介绍了从组件A1 C级实现了从组件A2接口。当使用C,则需要同时引用A1和A2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是由设计,根据<一个答案href=\"http://stackoverflow.com/questions/1810104/use-types-referenced-by-a-referenced-project-in-c\">this SO质疑

但我真的不想引用这两个组件,感觉就像我打破封装(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)


  1. 在A1,而不是引用A2组装,在Visual Studio中,右键单击A1项目,然后选择添加现有项。选择接口文件。 pressing添加按钮之前,请注意这是一个下拉按钮:放弃它并选择添加为链接。如果你不这样做,这样,你就必须十个分量接口,这是一个麻烦的多个副本

  1. 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组件定义的类型。我不认为危及manteinance,因为该接口只有一个来源$ C ​​$ C文件仍然被定义。好吧,它的编译到两个组件,所以这是一种二进制复制,但我不认为这是很糟糕...你?

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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