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

查看:131
本文介绍了接口在不同的项目类的实现分开吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在一个中等规模的项目(3开发商超过6个月以上)工作,需要做出以下决定:我们希望有从具体实施分离接口。首先是接口存储在一个单独的文件中。

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.

我们想更进一步,该数据更是分开:我们希望有一个项目(的csproj)与在一个.CS文件加上其他的接口。与帮助类CS文件(这样的接口中使用的一些公共课,一些枚举等)。然后,我们希望有另一个项目(的csproj)与工厂模式,混凝土界面执行和其他工人类。

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.

该解决方案有一个很大的缺点:它乘以2组件的数量,因为你必须为每一个正常的项目一期与interace和一个与实施项目。

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. 独立接口的目的,你可以不谈论描述项目的其余部分。在一个专门的接口组件,这可能是由项目中的所有其它组件引用把这些。典型的例子: ILogger IFileSystem IServiceLocator

  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.

类加上接口这真的只是必要在项目类的上下文。在相同的程序集,因为它们被连接到班把这些

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.

一个例子:假设你的域模型有一个香蕉类。如果检索通过 IBananaRepository 接口香蕉,那么该接口是紧耦合的香蕉。这是不可能实现或使用该接口不知道一些关于香蕉。因此,这是该接口位于同一组装为香蕉合乎逻辑的。

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.

上面的例子有一个技术耦合,但耦合可能只是一个逻辑一。例如, IFecesThrowingTarget 界面可能才有意义为合作者的类的即使接口声明没有技术链接

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.

这篇关于接口在不同的项目类的实现分开吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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