Does an empty base class design have a bad design?

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

But DTO classes have nothing to do. These 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> { ... } 
+7
c # interface application-design
source share
4 answers

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

In any case, having a base class means that when (well, if) you identify what they need in order to have something in common later, you do not need to go back and refactor (reinstall) everything. But in any case, I would prefer to use an interface rather than a base class for this kind of thing, since it does not seem like you need to reuse the base implementation (since they have nothing in common yet!). It will depend on what, in your opinion, may end with him later.

+6
source share

This is not a bad design, although somewhat unusual. Think of it this way: there are at least two advantages, while there are no disadvantages:

  • The base class serves as a filter for your interfaces, so you cannot pass them any object - only your DTO objects. Token interfaces can also be used for this.
  • When they eventually get something in common, it will be easy to add there, and you won’t have to reorganize everything.
+6
source share

The .NET coding instructions say that having an empty base class or interface (also called a tag interface) is really bad style. The preferred style is to use an attribute instead of commenting on classes of the same type. There is also an FxCop rule to enforce this agreement.

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

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

Good, self-documenting code profits from a limited interface.

+2
source share

In java, this is called a tag interface . The link provides a good background on tag interfaces, their use and problems.

+1
source share

All Articles