是有一个空的基类,糟糕的设计?是有、糟糕

2023-09-03 03:32:14 作者:醉酒思红颜

我需要一个基类为我的DTO类,它们将在我的泛型接口可以使用。

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

 公共无效GetGridData()
{

   IDataForGrid< D​​TOBase> AA;

   如果(要求== 1)AA =新CustomerGridData;
   如果(要求== 2)AA =新OrderGridData;

   VAR科尔= aa.GetList();
}

公共类CustomerGridData:IDataForGrid< CustomerDTO>
{
  ...
}
 

解决方案

如果他们的没有的共同之处,你会怎样做你从你的列表中检索实例?

别挣扎了,超过3条,你就是该去旅行了

在任何情况下,其基类是指当(好吧,如果的),你确定这是他们的执行的必须共同以后,你不要回去和重构(重新基地)的一切。不过,我会考虑使用一个接口,而不是一个基类,这样的事情在任何情况下,因为它听起来并不像有一个强烈的需要重用的底层实现(因为他们没有任何共同之处呢!)。这将取决于你认为他们可能最终不得不在共同以后。

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>
{
  ...
}

解决方案

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.