CQRS & PurchaseOrder AggregateRoot
本文关键字:PurchaseOrder AggregateRoot amp CQRS | 更新日期: 2023-09-27 17:57:09
我是CQRS和DDD的新手,为了练习,我正在使用MS WideWorldImporters数据库,目前我被困在采购订单下达命令上,说实话我不知道它应该是什么样子,你能指导我吗? 我应该有 1 个 PlaceOrderCommand 和处理程序应该使用它来创建采购订单填写其订单行并保存。如果是,当域模型如下所示时,命令应如下所示:
示例域模型:
public class PurchaseOrder : AggregateRoot<int>
{
private PurchaseOrder(int id, int supplierID, DateTime orderDate, int deliveryMethodID, int contactPersonID) : base(id)
{
SupplierID = supplierID;
OrderDate = orderDate;
DeliveryMethodID = deliveryMethodID;
ContactPersonID = contactPersonID;
}
public List<PurchaseOrderLine> PurchaseOrderLines { get; private set; }
public int SupplierID { get; private set; }
public DateTime OrderDate { get; private set; }
public int DeliveryMethodID { get; private set; }
public int ContactPersonID { get; private set; }
public static PurchaseOrder CreatePurchaseOrder(int supplierID, int deliveryMethodID, int contactPersonID)
{
return new PurchaseOrder(1, supplierID, DateTime.Now, deliveryMethodID, contactPersonID);
}
public void AddPurchaseOrderLine(int stockItemID)
{
PurchaseOrderLines.Add(new PurchaseOrderLine(this.Id, stockItemID));
}
}
示例命令处理程序:
public class PurchaseOrdersCommandHandler :
ICommandHandler<PlacePurchaseOrder>
{
private readonly IPurchaseOrderRepository purchaseOrderRepository;
public PurchaseOrdersCommandHandler(IPurchaseOrderRepository purchaseOrderRepository)
{
this.purchaseOrderRepository = purchaseOrderRepository;
}
public void Handle(PlacePurchaseOrder command)
{
// TODO: based on command Create Purchase order with its line items and save it to db as a whole
}
}
你能不能做这样的事情?
public void Handle(PlacePurchaseOrder command)
{
var purchaseOrder = PurchaseOrder.CreatePurchaseOrder(command.id, command.upplierID, command.orderDate, command.deliveryMethodID, command.contactPersonID);
foreach(int stockItemID in command.OrderLines)
{
purchaseOrder.AddPurchaseOrderLine(stockItemID)
}
this.purchaseOrderRepository.Save(purchaseOrder);
}