Slide 7
Slide 7 text
一般的寫法
建構式中做完初始化所有功能。
public class Order : EntityBase, IAggregateRoot
{
public decimal Total { get; private set; }
public Order(Guid id, IEnumerable orderItems) : base(id)
{
// 針對 Products 檢核(略)
// 計算總金額
var total = 0m;
foreach (var item in orderItems)
{
if (item.UnitPrice > 0)
{
total += item.UnitPrice;
}
else
{
throw new ArgumentOutOfRangeException(nameof(item.UnitPrice));
}
}
Total = total;
}
}