Search code examples
c#refactoringsolid-principles

Model a date time range object hierarchy


Newbie to code refactoring, I have a simple object as follow

public class TimeRange
    {
        public Guid Id { get; set; }

        public DateTime StartTime { get; set; }

        public DateTime EndTime { get; set; }

    }

    public class ActTimeRange : TimeRange
    {

    }

    public class PlannedTimeRange : TimeRange
    {

    }

I am thinking should if I should shrink this class hierarchy relationship by cutting down the two children classes and instead replace them with a boolean property on the parent class (i.e IsPlanned), which one would be better performance wise?


Solution

  • Because you don't seem to be adding any other properties to ActTimeRange and PlannedTimeRange it might be better to do this kind of model:

    public class TimeRange
    {
        public Guid Id { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public TimeRangeStatus Status { get; set; } = TimeRangeStatus.None;
    }
    
    public enum TimeRangeStatus
    {
        None,
        Actual,
        Planned
    }
    

    I would avoid bool as a property or field as you can end up with method calls like DoSomething(true, false) - which are not very clear. It's better to have DoSomething(TimeRangeStatus.Actual, SomeOtherFlag.Red).

    Performance should not be a concern with any approach.