我解决这个问题的方法是有 3 个表(一年中的一天、一周中的一天和绝对日期),它们覆盖包含每个规则的开始/结束时间的 DateConstraint。
验证码比较简单。我将 Matches() 例程添加到所有类。
public partial class DateConstraint
{
public virtual bool Matches()
{
return DateTime.Now.TimeOfDay.Ticks >= StartTime.Ticks && DateTime.Now.TimeOfDay.Ticks <= EndTime.Ticks;
}
}
public partial class DayOfWeekConstraint : DateConstraint
{
public override bool Matches()
{
return base.Matches() && this.DayOfWeek == Convert.ToInt16(DateTime.Now.DayOfWeek);
}
}
public partial class DayOfYearConstraint : DateConstraint
{
public override bool Matches()
{
return base.Matches() && Date.Day == DateTime.Now.Day && Date.Month == DateTime.Now.Month;
}
}
public partial class AbsoluteDateConstraint : DateConstraint
{
public override bool Matches()
{
return base.Matches() && Date.Day == DateTime.Now.Day && Date.Month == DateTime.Now.Month && Date.Year == DateTime.Now.Year;
}
}
要进行验证,我只需要做
foreach(var dateConstraint in Restaurant.DateConstraints)
{
if(!dateConstraint.Matches()) { return false; }
}