我担心我添加了太多接口 [英] I'm worried I'm adding too many interfaces

查看:145
本文介绍了我担心我添加了太多接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的域模型并继续重构它。像我一样,我发现我喜欢接口,因为它允许我根据接口为具体类型创建可重用的方法/控制器/视图。但是,我发现每次向其中一个域实体添加新属性时都会创建一个接口。

I am building out my domain model and continuing to refactor it. As I do, I am finding that I like interfaces as it allows me to create reusable methods/controllers/views for concrete types based on their interfaces. However, I am finding that I am creating an interface every time I add a new property to one of my domain entities.

例如,我有一个 MemberStatus 对象,它继承自一个抽象的 Entity 对象,后者又实现了 IIdentifiableEntity 接口意味着它具有Id属性。 MemberStatus还实现了 INamedEntity 接口,这意味着它具有Name属性, IOrderedEntity 接口意味着它具有DisplayOrder属性和 IHasMembers 接口含义它有一个集合成员对象。这是代码:

For example, I have a MemberStatus object which inherits from an abstract Entity object which in turn implements the IIdentifiableEntity interface meaning that it has an Id property. MemberStatus also implements the INamedEntity interface meaning that it has a Name property, the IOrderedEntity interface meaning that it has a DisplayOrder property and the IHasMembers interface meaning that it has a collection Member objects. Here's the code:

public class MemberStatus : Entity, INamedEntity, IOrderedEntity, IHasMembers
{
  public string Name { get; set; }
  public float DisplayOrder { get; set; }
  public ICollection<Member> Members { get; set; }
}

public abstract class Entity : IIdentifiableEntity
{
  public int Id { get; set; }
}

public interface IIdentifiableEntity
{
  int Id { get; set; }
}

public interface INamedEntity
{
  string Name { get; set; } 
}

public interface IOrderedEntity
{
  float DisplayOrder { get; set; }
}

public interface IHasMembers
{
  ICollection<Member> Members { get; set; }
}

现在,这似乎可以正常工作,因为我有其他类似的对象,如 MemberPosition MemberTeam ,它们都实现了这些相同的接口,我可以将我的存储库方法和控制器操作与实现这些接口的泛型一起使用,并且需要大量代码重用。

Now, this seems to work fine as I other similar objects such as MemberPosition and MemberTeam which all implement these same interfaces and I can use my repository methods and controller actions with generics that implement these interfaces and have a lot of code reuse.

但是,我担心的是每次向具体对象添加新属性时是否适合继续添加简单的单属性接口。例如,假设我要添加 bool Enabled 属性...我应该继续创建 IEnabled 界面吗?我问的原因是一些使用泛型的控制器初始化器变得非常长,如下面的代码行所示。这是正常和最佳做法吗?

However, my concern is whether or not it's appropriate to keep adding simple, one-property interfaces every time I add a new property to my concrete objects. For example, let's say I want to add a bool Enabled property... should I continue to create a IEnabled interface? The reason I'm asking is that some of controller "initializers" that are using generics are becoming very long as shown in the following line of code. Is this normal and best-practice?

public abstract class OrderedCrudController<TEntity> : CrudController<TEntity> where TEntity : Entity, INamedEntity, IOrderedEntity, IHasMembers, new()


推荐答案

您使用接口这一事实是一件好事。但是,您应该问问自己,如果我创建一个 IEnabled 界面,我是否会仅通过该界面引用我的类?即,是否存在我与我的类完全交互的上下文纯粹通过接口公开的单个属性?

The fact that you are using interfaces is a good thing. However, you should ask yourself, if I create an IEnabled interface, will I ever reference my class by that interface alone? i.e. will there be contexts where I interact with my class purely via the single property that interface exposes?

此外,你能考虑将与多个实现交互的上下文 IEnabled interface?

Also, can you consider contexts where you will interact with multiple implementation of this IEnabled interface?

如果这两个问题的答案都是否,那么界面服务很少目的。

If the answer to both of these question is "no", then the interface serves very little purpose.

话虽如此,请不要过于担心!它的伤害很小。

Having said that, please don't worry too much about this! it does very little harm.

这篇关于我担心我添加了太多接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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