CQRS 和 PurchaseOrder 聚合根
CQRS & PurchaseOrder AggregateRoot
我是 CQRS 和 DDD 的新手,为了练习我正在使用 MS WideWorldImporters 数据库,目前我被困在采购订单放置命令上,说实话我不知道它应该是什么样子,你能指导我吗?
我应该有 1 个 PlaceOrderCommand 并且处理程序应该使用它来创建 PurchaseOrder 填充它的 OrderLines 然后保存。如果是,当域模型看起来像这样时命令应该是什么样子:
示例域模型:
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);
}
我是 CQRS 和 DDD 的新手,为了练习我正在使用 MS WideWorldImporters 数据库,目前我被困在采购订单放置命令上,说实话我不知道它应该是什么样子,你能指导我吗? 我应该有 1 个 PlaceOrderCommand 并且处理程序应该使用它来创建 PurchaseOrder 填充它的 OrderLines 然后保存。如果是,当域模型看起来像这样时命令应该是什么样子:
示例域模型:
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);
}