接口与类的实现分离在不同的项目中?[英] Interfaces separated from the class implementation in separate projects?

本文是小编为大家收集整理的关于接口与类的实现分离在不同的项目中?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我们从事一个中型项目(超过6个月的3个开发人员),需要做出以下决定:我们希望将接口与具体实施分开.首先是将接口存储在单独的文件中.

我们想进一步分开数据:我们想在一个.cs文件和一个带有帮助类的一个.cs文件和另一个.cs文件中拥有一个项目(CSPROJ)(例如内部使用的某些公共类此界面,一些枚举等).然后,我们希望拥有一个具有出厂模式,混凝土接口实现和其他" Worker"类的项目(CSPROJ).

任何想要创建对象实现此接口的类都必须包括包含接口和公共类的第一个项目,而不是实现本身.

该解决方案具有一个很大的缺点:它将组件数量乘以2,因为您将拥有每个具有Interace和一个实施的"正常"项目一个项目.

您建议什么?您认为将所有接口放在一个单独的项目中而不是一个界面中的一个界面是一个好主意?

推荐答案

我会区分这样的接口:

  1. 独立界面您可以在不谈论其余项目的情况下描述其目的.将它们放入单个专用的"接口组件"中,您的项目中所有其他组件都可能引用.典型示例:ILogger,IFileSystem,IServiceLocator.

  2. 类耦合接口实际上只有在项目类的上下文中才有意义.将它们放在与与之耦合的类的同一组件中.

    一个示例:假设您的域模型具有Banana类.如果您通过IBananaRepository接口检索香蕉,则该界面与香蕉紧密耦合.在不知道有关香蕉的情况下,实现或使用界面是不可能的.因此,界面驻留在与Banana

    的同一组件中是合乎逻辑的.

    上一个示例具有技术耦合,但耦合可能只是合乎逻辑的耦合.例如,即使接口声明没有技术链接到Monkey.

我的答案确实取决于这样的观念,即可以与课堂结合使用.隐藏在接口后面的所有内容将是一个错误. 有时候只能" new Up"一个类别,而不是通过工厂注射或创建它.

其他推荐答案

是的,我认为这是一个好主意.实际上,我们一直在这里这样做,最终由于一个简单的原因而必须这样做:

我们使用远程访问服务器功能.因此,服务器上的远程对象需要实现接口,客户端代码必须访问接口才能使用远程对象.

通常,我认为当将接口放在单独的项目中时,您会更加松散地耦合,所以只要继续前进即可.拥有2个组件并不是一个问题,是吗?

加法:

刚想到:通过将接口放在单独的组件中,您还可以获得能够重复使用接口的好处,如果其中一些足够通用.

其他推荐答案

除非它为您的应用程序的架构提供了可靠的好处,否则我不会这样做.

很好地保持您的组装数量创建.即使界面及其实现在同一组件中,您仍然可以通过一些纪律正确地实现自己的脱钩.

本文地址:https://www.itbaoku.cn/post/627369.html

问题描述

We work on a middle-size project (3 developers over more than 6 months) and need to make following decision: We'd like to have interfaces separated from concrete implementation. The first is to store the interface in a separate file.

We'd like to go further and separate the data even more: We'd like to have one project (CSPROJ) with interface in one .CS file plus another .CS file with help classes (like some public classes used within this interface, some enums etc.). Then, we'd like to have another project (CSPROJ) with a factory pattern, concrete interface implementation and other "worker" classes.

Any class which wants to create an object implementing this interface must include the first project which contains the interfaces and public classes, not the implementation itself.

This solution has one big disadvantage: it multiplies the number of assemblies by 2, because you would have for every "normal" project one project with interace and one with implementation.

What would you recommend? Do you think it's a good idea to place all interfaces in one separate project rather than one interface in its own project?

推荐答案

I would distinguish between interfaces like this:

  1. Standalone interfaces whose purpose you can describe without talking about the rest of your project. Put these in a single dedicated "interface assembly", which is probably referenced by all other assemblies in your project. Typical examples: ILogger, IFileSystem, IServiceLocator.

  2. Class coupled interfaces which really only make sense in the context of your project's classes. Put these in the same assembly as the classes they are coupled to.

    An example: suppose your domain model has a Banana class. If you retrieve bananas through a IBananaRepository interface, then that interface is tightly coupled to bananas. It is impossible to implement or use the interface without knowing something about bananas. Therefore it is only logical that the interface resides in the same assembly as Banana.

    The previous example has a technical coupling, but the coupling might just be a logical one. For example, a IFecesThrowingTarget interface may only make sense as a collaborator of the Monkey class even if the interface declaration has no technical link to Monkey.

My answer does depend on the notion that it's okay to have some coupling to classes. Hiding everything behind an interface would be a mistake. Sometimes it's okay to just "new up" a class, instead of injecting it or creating it via a factory.

其他推荐答案

Yes, I think this is a good idea. Actually, we do it here all the time, and we eventually have to do it because of a simple reason:

We use Remoting to access server functionality. So the Remote Objects on the server need to implement the interfaces and the client code has to have access to the interfaces to use the remote objects.

In general, I think you are more loosely coupled when you put the interfaces in a separate project, so just go along and do it. It isn't really a problem to have 2 assemblies, is it?

ADDITION:

Just crossed my mind: By putting the interfaces in a separate assembly, you additionally get the benefit of being able to reuse the interfaces if a few of them are general enough.

其他推荐答案

I wouldn't do it unless it offers a proven benefit for your application's architecture.

It's good to keep an eye on the number of assemblies you're creating. Even if an interface and its implementation are in the same assembly, you can still achieve the decoupling you rightly seek with a little discipline.