C# property exactly the same, defined in two places

Posted by Sarah Vessels on Stack Overflow See other posts from Stack Overflow or by Sarah Vessels
Published on 2010-04-23T16:58:39Z Indexed on 2010/04/23 17:03 UTC
Read the original article Hit count: 142

Filed under:
|
|
|

I have the following classes:

  1. Defect - represents a type of data that can be found in a database
  2. FilterQuery - provides a way of querying the database by setting simple Boolean filters

Both Defect and FilterQuery implement the same interface: IDefectProperties. This interface specifies particular fields that are in the database. Different classes have methods that return lists of Defect instances. With FilterQuery, you specify some filters for the particular properties implemented as part of IDefectProperties, and then you run the query and get back a list of Defect instances.

My problem is that I end up implementing some properties exactly the same in FilterQuery and Defect. The two are inherently different classes, they just share some of the same properties. For example:

public DateTime SubmitDateAsDate
{
    get { return DateTime.Parse(SubmitDate); }
    set { SubmitDate = value.ToString(); }
}

This is a property required by IDefectProperties that depends on a different property, SubmitDate, which returns a string instead of a DateTime. Now SubmitDate is implemented differently in Defect and FilterQuery, but SubmitDateAsDate is exactly the same. Is there a way that I can define SubmitDateAsDate in only place, but both Defect and FilterQuery provide it as a property? FilterQuery and Defect already inherit from two different classes, and it wouldn't make sense for them to share an ancestor anyway, I think. I am open to suggestions as to my design here as well.

© Stack Overflow or respective owner

Related posts about c#

Related posts about properties