空基类是不是不好的设计?

发布于 2024-08-24 07:50:31 字数 362 浏览 11 评论 0原文

我的 DTO 类需要一个基类,它将在我的通用接口中使用。

但 DTO 类没有任何共同点。它们只是包含一些属性的愚蠢类。

public void GetGridData()
{

   IDataForGrid<DTOBase> aa;

   if(request == 1) aa = new CustomerGridData;
   if(request == 2) aa = new OrderGridData;

   var coll = aa.GetList();
}

public class CustomerGridData : IDataForGrid<CustomerDTO>
{
  ...
}

I need a base class for my DTO classes which will be used in my generic interfaces.

But the DTO classes have nothing in common. They are just dumb classes containing some properties.

public void GetGridData()
{

   IDataForGrid<DTOBase> aa;

   if(request == 1) aa = new CustomerGridData;
   if(request == 2) aa = new OrderGridData;

   var coll = aa.GetList();
}

public class CustomerGridData : IDataForGrid<CustomerDTO>
{
  ...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

起风了 2024-08-31 07:50:31

如果它们没有任何共同点,您将如何处理从列表中检索到的实例?

无论如何,拥有基类意味着当(好吧,如果)你发现它们确实稍后需要有共同点时,你不必去返回并重构(re-base)一切。但在任何情况下,我都会考虑使用接口而不是基类来处理此类事情,因为听起来并不强烈需要重用底层实现(因为它们还没有任何共同点!)。这取决于你认为他们以后可能有什么共同点。

If they have nothing in common, what are you going to do with the instances you retrieve from your list?

In any case, having the base class means that when (okay, if) you identify something they do need to have in common later on, you don't have to go back and refactor (re-base) everything. But I'd consider using an interface rather than a base class for this sort of thing in any case, as it doesn't sound like there's a strong need to reuse underlying implementation (as they have nothing in common yet!). It'll depend on what you think they may end up having in common later on.

月下凄凉 2024-08-31 07:50:31

尽管有些不常见,但这并不是一个糟糕的设计。可以这样想 - 至少有两个好处,但没有缺点:

  • 基类充当接口的过滤器,这样您就不能只向它们传递任何对象 - 只能向它们传递 DTO 对象。标记接口也可以做到这一点。
  • 当他们最终确实获得一些共同点时,将很容易将其添加到那里,并且您不必重构所有内容。

It is not a bad design, although somewhat uncommon. Think of it this way - there are at least two benefits while there are no drawbacks:

  • The base class serves as a filter for your interfaces so that you can't pass just any object to them - just your DTO objects. Marker interfaces could to this too.
  • When they eventually do get something in common, it will be easy to add it there and you won't have to refactor everything.
笔芯 2024-08-31 07:50:31

.NET 编码指南指出,拥有空基类或接口(也称为“标签”接口)确实是不好的风格。首选的样式是使用属性来注释同类的类。还有一个要强制执行的 FxCop 规则本次公约。

但是,当公共基类可用于表示公共层次结构(即使不存在公共功能)时,我有时(在极少数情况下)会使用此习惯用法。属性不能用于此目的。

例如,在编程语言的解释器中,多个方法返回特殊的基类Value,即在该编程语言内具有值的东西。基本上,这个值可以是从数字到字符串的一切(它们是特殊类,而不是System.Int32System.String)到一个复合对象。我也可以返回 System.Object,但这会使我的公共接口的类型变弱。

良好的自记录代码受益于受限接口。

The .NET coding guidelines say that having an empty base class or interface (also called “tag” interface) is indeed bad style. The preferred style is to use an attribute instead to annotate classes of the same kind. There is also an FxCop rule to enforce this convention.

However, I sometimes (in rare cases) use this idiom when a common base class is useful to denote a common hierarchy even if no common functionality exists. Attributes cannot be used for this.

For example, in an interpreter for a programming language several methods return a special base class Value, i.e. something which has a value inside that programming language. Basically, this value can be everything from a number to a string (which, are special classes, not System.Int32 or System.String) to a composite object. I could also return System.Object but this would make the typing of my public interface weaker.

Good, self-documenting code profits from the restricted interface.

私野 2024-08-31 07:50:31

在 Java 中,这称为 标签接口。该链接提供了一些有关标签接口、其用途和问题的良好背景知识。

In java this is called a Tag Interface. The link provides some good background on tag interfaces, their uses and issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文