我想你可以在內部表示以此爲開始的日期時間(它給你的星期幾),然後結束時間跨度,但它們公開爲三個屬性,或者是DateTime是否或時間跨度
我們開關在一個更DDD的帽子上......你「可以」使用TimeSpan對象來表示一天中的某個時間,但如果我正在考慮DDD條款,我想要考慮商店打開的時間。 TimeSpans可以代表更多的信息,並且每次與他們打交道時都必須特別驗證他們不超過一天的時間。
所以一種方法是創建一個簡單的類來表示您想表示的內容(您可以只包裝一個TimeSpan對象而不是使用幾個整數)。
public struct Time
{
private readonly int _hour;
private readonly int _minute;
private readonly int _second;
public Time(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24)
throw new ArgumentOutOfRangeException("hour", "Hours must be between 0 and 23 inclusive");
if (minute < 0 || minute > 59)
throw new ArgumentOutOfRangeException("minute", "Minutes must be between 0 and 23 inclusive");
if (second < 0 || second > 59)
throw new ArgumentOutOfRangeException("second", "Seconds must be between 0 and 23 inclusive");
_hour = hour;
_minute = minute;
_second = second;
}
public Time(Time time)
: this(time.Hour, time.Minute, time.Second)
{
}
public int Hour { get { return _hour; } }
public int Minute { get { return _minute; } }
public int Second { get { return _second; } }
public override string ToString()
{
return ToString(true);
}
public string ToString(bool showSeconds)
{
if (showSeconds)
return string.Format("{0:00}:{1:00}:{2:00}", Hour, Minute, Second);
return string.Format("{0:00}:{1:00}:{2:00}", Hour, Minute);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (obj.GetType() != typeof (Time)) return false;
return Equals((Time) obj);
}
public bool Equals(Time other)
{
return other._hour == _hour && other._minute == _minute && other._second == _second;
}
public override int GetHashCode()
{
unchecked
{
int result = _hour;
result = (result*397)^_minute;
result = (result*397)^_second;
return result;
}
}
}
public class OpeningHours
{
public DayOfWeek DayOfWeek { get; set; }
public Time OpeningTime { get; set; }
public Time ClosingTime { get; set; }
public OpeningHours(DayOfWeek dayOfWeek, Time openingTime, Time closingTime)
{
DayOfWeek = dayOfWeek;
OpeningTime = openingTime;
ClosingTime = closingTime;
}
}
我建議您將類「OpeningTime」重命名爲「OperationHours」 – 2010-08-25 15:33:48
。您在.Net中比在SQL中具有更大的靈活性。想想你想如何將它堅持到表格中,然後在.Net中模擬它(假設你僅僅迎合一個RDBMS,並且這種方式會持續一段時間)。這次你想要多精確? – 2010-08-25 15:39:50