Hi, still experimenting with Aurelius I´ve encountered myself with a question. Let´s suppose we have the following classes (simplified for example purposes):
TSalesByDay = class;
[Entity]
[Automapping]
TSale = class
private
fID: Integer;
fTotal: Currency;
[Association([TAssociationProp.Lazy, TAssociationProp.Required], [TCascadeType.SaveUpdate])]
[JoinColumn('ID_SALEDAY', [TColumnProp.Required])]
fSaleDay : Proxy<TSalesByDay>;
public
property ID: Integer read fID write fID;
property Total: Currency read fTotal write fTotal;
property SaleDay : TSalesByDay read fSaleDay;
end;
[Entity]
[Automapping]
TSalesByDay = class
private
fID: Integer;
fDate: TDateTime;
fTotalSold : Currency;
[ManyValuedAssociation([TAssociationProp.Lazy], CascadeTypeAll, 'fSaleDay')]
FSales: Proxy<TList<TSale>>;
protected
function GetSales() : TList<TSale>;
public
property ID: Integer read fID write fID;
property Date: TDateTime read fDate write fDate;
property TotalSold: Currency read fTotalSold;
property Sales : TList<TSale> read GetSales;
end;
In this case, what is the best method to save a new sale?:
Approach A:
TodaysSales := Manager.Find<TSalesByDay>(1);
NewSale := TSale.Create;
NewSale.Total := 100;
NewSale.SaleDay := TodaysSales;
Manager.Save(NewSale);
Approach B:
TodaysSales := Manager.Find<TSalesByDay>(1);
NewSale := TSale.Create;
NewSale.Total := 100;
TodaysSales.Sales.Add(NewSale);
Manager.Save(TodaySales);
NOTE: Since sometimes there´s is A LOT of sales records I added the "TotalSold" property to the TSalesByDay class to obtain in the instant the total amount sold in that day, and also to perform queries such as "How much was sold in the past 3 years" in which I just query the TSalesByDay table/class instead of performing a SUM for all the TSale records.
So, my question is how to auto-sum / update the "TotalSold" property in the TSalesByDay class every time a new TSale is added to the list or saved to the database? (I want to do this:
TodaySales.TotalSold := TodaySales.TotalSold + NewSale.Total;
But automatically everytime a new TSale is saved.
Any help/recommendations in the design of this classes would be appreciated.
Thanks in advance.