为什么在设计Model的时候会经常出现我中有你你中有我的设计?
先看一个例子
Event class
[code=C#]
public class Event
{
public Event()
{
ReservedTickets = new List <TicketReservation> ();
PurchasedTickets = new List <TicketPurchase> ();
}
public Guid Id { get; set; }
public string Name { get; set; }
public int Allocation { get; set; }
public List <TicketReservation> ReservedTickets { get; set; }
public List <TicketPurchase> PurchasedTickets { get; set; }
public int AvailableAllocation()
{
int salesAndReservations = 0;
PurchasedTickets.ForEach(t => salesAndReservations += t.TicketQuantity);
ReservedTickets.FindAll(r => r.StillActive()).ForEach(r => salesAndReservations += r.TicketQuantity);
return Allocation - salesAndReservations;
}
public bool CanPurchaseTicketWith(Guid reservationId)
{
if (HasReservationWith(reservationId))
return GetReservationWith(reservationId).StillActive();
return false;
}
public TicketPurchase PurchaseTicketWith(Guid reservationId)
{
if (!CanPurchaseTicketWith(reservationId))
throw new ApplicationException(DetermineWhyATicketCannotbePurchasedWith(reservationId));
TicketReservation reservation = GetReservationWith(reservationId);