The C # property is exactly the same as defined in two places

I have the following classes:

  • Defect - represents the type of data that can be found in the database
  • FilterQuery - provides a way to query the database by installing simple boolean filters

Both Defectand FilterQueryimplement the same interface IDefectProperties. This interface indicates the specific fields that are in the database. Different classes have methods that return lists of instances Defect. With the help of FilterQueryyou specify some filters for specific properties implemented as part IDefectProperties, and then run a query and return a list of instances Defect.

My problem is that I end up implementing some properties in exactly the same way in FilterQueryand Defect. These two are essentially different classes, they just share the same properties. For instance:

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

This is the property required IDefectProperties, which depends on another property SubmitDatethat returns stringinstead DateTime. Now SubmitDateimplemented differently in Defectand FilterQuery, but SubmitDateAsDateexactly the same. Is there any way that I can identify SubmitDateAsDateonly one location, but both Defectand FilterQuerygive it as a property? FilterQueryand Defectthey are already inheriting from two different classes, and, it seems to me, it would not make sense for them to share with their ancestor. I am also open to suggestions regarding my design here.

+3
6

, (, , ) .

- #, ( " , -" ). , :

public static DateTime GetSubmitDateAsDate(this IDefectProperties defectProperties) {
    return DateTime.Parse(defectProperties.SubmitDate);
}

public static void SetSubmitDateAsDate(this IDefectProperties defectProperties, DateTime dateTime) {
    defectProperties.SubmitDate = dateTime.ToString();
}
+2

, .

internal class DefectPropertyInternal
{
  private IDefectproperty dp
  public DefectPropertyInternal(IDefectproperty dp)
  {
     this.dp = dp;
  }

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

}

public class Defect : IDefectProperty
{
  private DefectPropertyInternal dpi;

  public Defect()
  {
     this.dpi = new DefectpropertyInternals(this);
  }

 public DateTime SubmitDateAsDate
 {
    get { return dpi.SubmitDateAsDate; }
    set { dpi.SubmitDateAsDate = value; }
 }

}

, DefectPropertyInternals FilterQuery Defect, .

+5

, , .

. , , , , , . , , , , , , FilterQuery, ( ) Defect Defect . , , - "", , .

+2

@Fyodor Soikin. , Defect FilterQuery. , ... AsDate . , , , . , , , / , .

public static class DefectExtensions
{
     public static DateTime SubmitDateAsDate( this IDefectProperties source )
     {
            return DateTime.Parse( source.SubmitDate );
     }

     public static void SetSubmitDateAsDate( this IDefectProperties source, DateTime date )
     {
           source.SubmitDate = date.ToString();
     }
}
+1

. FilterQuery, IDefectProperties, , .

:

class FilterQuery{
    IDefectProperties defectProperties;
    FilterQuery(IDefectProperties dp){
        defectProperties=dp;
    }
}

FilterQuery ( FilterQuery) FilterQuery. "" - - ( ). FIlterQuery , .

+1

SubmitDateAsDate ?

0

Source: https://habr.com/ru/post/1742498/


All Articles