Tips - IObjectValidityDateRange Interface

The IObjectValidityDateRange interface can be used to validate entities on dates / date ranges.

Let's take an example

public class Product
{
    ...

    public DateTime? BeginSaleDate { get; set; }

    public DateTime? EndSaleDate { get; set; }
}

Implements the IObjectValidityDateRange interface

// Models | Product.custom.cs

public class Product : IObjectValidityDateRange
{
    #region [ IObjectValidityDateRange Implementation ]

    public DateTime? ValidityBeginDate
    {
        get { return this.BeginSaleDate; }
    }

    public DateTime? ValidityEndDate
    {
        get { return this.EndSaleDate; }
    }

    #endregion
}

Then

// Tests
		
Product p1 = new Product
{
    BeginSaleDate = new DateTime(2014, 1, 1),
    EndSaleDate = new DateTime(2014, 12, 31)
};

Product p2 = new Product
{
    BeginSaleDate = new DateTime(2015, 1, 1)
};

// Date validation

p1.IsValidityDateRangeValid(new DateTime(2015, 7, 1)); // false
p2.IsValidityDateRangeValid(new DateTime(2015, 7, 1)); // true

// Range validation

p1.IsValidityDateRangeValid(new DateTime(2014, 7, 1), new DateTime(2015, 7, 31)); // true
p1.IsValidityDateRangeValid(new DateTime(2015, 7, 1), new DateTime(2015, 7, 31)); // false
p2.IsValidityDateRangeValid(new DateTime(2016, 1, 1), new DateTime(2016, 9, 31)); // true