领域驱动设计实战进阶第一波(十三):订单上下文领域逻辑
DDD实战进阶第一波(十三):开发一般业务的大健康行业直销系统(订单上下文领域逻辑)
前一篇文章主要讲了订单上下文的POCO模型,其中订单与订单项中有大量的值对象。这篇文章主要讲讲这些值对象以及订单项、订单相关的领域逻辑。
1.ProductSKUs值对象领域逻辑:
ProductSKUs值对象用于订单项实体中,它的信息应该来源于产品上下文的ProductSKU实体。
publicpartialclass ProductSKUs
{
public ProductSKUs() { }
public ProductSKUs CreateProductSKUs(ProductSKU productsku)
{
this.ProductSPUName = productsku.ProductSPUName;
this.ProductPrice = productsku.DealerPrice;
this.ProductPV = productsku.PV;
this.ProductSKUId = productsku.Id;
returnthis;
}
}
2.OrderItemTotalPV值对象领域逻辑:
OrderItemTotalPV值对象的领域逻辑主要是通过订单项购买的ProductSKU的PV与数量计算出这个订单项的总PV值。
publicpartialclass OrderItemTotalPV
{
public OrderItemTotalPV() { }
publicOrderItemTotalPV CreateOrderItemTotalPV(ProductSKU productsku,int count)
{
this.SubTotalPV = productsku.PV * count;
returnthis;
}
}
3.OrderItemTotalPrice值对象领域逻辑:
OrderItemTotalPrice值对象的领域逻辑主要是通过订单项购买的ProductSKU的单价与数量计算出这个订单项的总价。
publicpartialclass OrderItemTotalPrice
{
public OrderItemTotalPrice() { }
publicOrderItemTotalPrice CreateOrderItemTotalPrice(ProductSKU productsku,int count)
{
this.SubTotalPrice = productsku.DealerPrice * count;
returnthis;
}
}
4.OrderItem实体的领域逻辑:
OrderItem实体的领域逻辑主要包含两个方面,一个是OrderItem的Code生成规则,二是调用自身包含的上述三个值对象领域逻辑,生成相关的值对象。
publicpartialclass OrderItem
{
public OrderItem() { }
publicOrderItem CreateOrderItem(ProductSKU productsku,int count)
{
this.Id = Guid.NewGuid();
this.Code ="OrderItem "+ DateTime.Now.ToString();
this.Count = count;
this.OrderItemTotalPrice =new OrderItemTotalPrice().CreateOrderItemTotalPrice(productsku,
count);
this.OrderItemTotalPV =new OrderItemTotalPV().CreateOrderItemTotalPV(productsku,
count);
this.ProductSKUs =new ProductSKUs().CreateProductSKUs(productsku);
returnthis;
}
}
5.OrderStreet值对象领域逻辑:
OrderStreet值对象的信息主要由产品购买时,确定的联系人信息将相关的属性赋值给OrderStreet值对象。
publicpartialclass OrderStreet
{
public OrderStreet() { }
public OrderStreet CreateOrderStreet(Contact contact)
{
this.Privince = contact.Province;
this.City = contact.City;
this.Zero = contact.Zero;
this.Street = contact.Street;
returnthis;
}
}
6.OrderTotalPV值对象领域逻辑:
OrderTotalPV值对象的值由每个订单项的OrderItemTotalPV值对象累加起来。
publicpartialclass OrderTotalPV
{
public OrderTotalPV() { }
publicOrderTotalPV CreateOrderTotalPV(List itemtotalpvs)
{
varordertotalpv =0.00M;
itemtotalpvs.ForEach(p => {
ordertotalpv += p.SubTotalPV;
});
this.TotalPV = ordertotalpv;
returnthis;
}
}
7.OrderTotalPrice值对象领域逻辑:
OrderTotalPrice值对象的值由每个订单项的OrderItemTotalPrice值对象累加起来。
publicpartialclass OrderTotalPrice
{
public OrderTotalPrice() { }
publicOrderTotalPrice CreateOrderTotalPrice(List itemtotalprices)
{
varordertotalprice =0.00M;
itemtotalprices.ForEach(p => {
ordertotalprice += p.SubTotalPrice;
});
this.TotalPrice = ordertotalprice;
returnthis;
}
}
8.Orders聚合根领域逻辑:
Orders聚合根其实就是协调OrderItem实体、自身包含的3个值对象完成整个下单的领域逻辑。
publicOrders CreateOrders(Guid id,Guid dealerid,List productskus,
List counts,Contact contact)
{
this.Id = id;
this.OrderDealerId = dealerid;
this.OrderDateTime = DateTime.Now;
this.Telephone = contact.ContactTel;
this.Code ="Order "+ DateTime.Now.ToString();
this.OrderStreet =new OrderStreet().CreateOrderStreet(contact);
this.OrderItems =newList();
varorderitemtotalprices =newList();
varorderitemtotalpvs =newList();
for(inti =0; i < productskus.Count; i++)
{
varorderitem =new OrderItem().CreateOrderItem(productskus[i], counts[i]);
this.OrderItems.Add(orderitem);
orderitemtotalprices.Add(orderitem.OrderItemTotalPrice);
orderitemtotalpvs.Add(orderitem.OrderItemTotalPV);
}
this.OrderTotalPrice =new OrderTotalPrice().CreateOrderTotalPrice(orderitemtotalprices);
this.OrderTotalPV =new OrderTotalPV().CreateOrderTotalPV(orderitemtotalpvs);
returnthis;
}
QQ讨论群:309287205
DDD实战进阶视频请关注微信公众号: