前一篇文章主要讲了订单上下文的POCO模型,其中订单与订单项中有大量的值对象。这篇文章主要讲讲这些值对象以及订单项、订单相关的领域逻辑。

1.ProductSKUs值对象领域逻辑:
ProductSKUs值对象用于订单项实体中,它的信息应该来源于产品上下文的ProductSKU实体。

public partial class ProductSKUs
{
public ProductSKUs() { }
public ProductSKUs CreateProductSKUs(ProductSKU productsku)
{
this.ProductSPUName = productsku.ProductSPUName;
this.ProductPrice = productsku.DealerPrice;
this.ProductPV = productsku.PV;
this.ProductSKUId = productsku.Id;
return this;
}
}

2.OrderItemTotalPV值对象领域逻辑:

OrderItemTotalPV值对象的领域逻辑主要是通过订单项购买的ProductSKU的PV与数量计算出这个订单项的总PV值。

public partial class OrderItemTotalPV
{
public OrderItemTotalPV() { }
public OrderItemTotalPV CreateOrderItemTotalPV(ProductSKU productsku,int count)
{
this.SubTotalPV = productsku.PV * count;
return this;
}
}

3.OrderItemTotalPrice值对象领域逻辑:

OrderItemTotalPrice值对象的领域逻辑主要是通过订单项购买的ProductSKU的单价与数量计算出这个订单项的总价。

 public partial class OrderItemTotalPrice
{
public OrderItemTotalPrice() { }
public OrderItemTotalPrice CreateOrderItemTotalPrice(ProductSKU productsku,int count)
{
this.SubTotalPrice = productsku.DealerPrice * count;
return this;
}
}

4.OrderItem实体的领域逻辑:

OrderItem实体的领域逻辑主要包含两个方面,一个是OrderItem的Code生成规则,二是调用自身包含的上述三个值对象领域逻辑,生成相关的值对象。

 public partial class OrderItem
{
public OrderItem() { }
public OrderItem 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);
return this;
}
}

5.OrderStreet值对象领域逻辑:

OrderStreet值对象的信息主要由产品购买时,确定的联系人信息将相关的属性赋值给OrderStreet值对象。

 public partial class OrderStreet
{
public OrderStreet() { }
public OrderStreet CreateOrderStreet(Contact contact)
{
this.Privince = contact.Province;
this.City = contact.City;
this.Zero = contact.Zero;
this.Street = contact.Street;
return this;
}
}

6.OrderTotalPV值对象领域逻辑:

OrderTotalPV值对象的值由每个订单项的OrderItemTotalPV值对象累加起来。

public partial class OrderTotalPV
{
public OrderTotalPV() { }
public OrderTotalPV CreateOrderTotalPV(List<OrderItemTotalPV> itemtotalpvs)
{
var ordertotalpv = 0.00M;
itemtotalpvs.ForEach(p =>
{
ordertotalpv += p.SubTotalPV;
});
this.TotalPV = ordertotalpv;
return this;
}
}

7.OrderTotalPrice值对象领域逻辑:

OrderTotalPrice值对象的值由每个订单项的OrderItemTotalPrice值对象累加起来。

 public partial class OrderTotalPrice
{
public OrderTotalPrice() { }
public OrderTotalPrice CreateOrderTotalPrice(List<OrderItemTotalPrice> itemtotalprices)
{
var ordertotalprice = 0.00M;
itemtotalprices.ForEach(p =>
{
ordertotalprice += p.SubTotalPrice;
});
this.TotalPrice = ordertotalprice;
return this;
}
}

8.Orders聚合根领域逻辑:

Orders聚合根其实就是协调OrderItem实体、自身包含的3个值对象完成整个下单的领域逻辑。

public Orders CreateOrders(Guid id,Guid dealerid,List<ProductSKU> productskus,
List<int> 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 = new List<OrderItem>();
var orderitemtotalprices = new List<OrderItemTotalPrice>();
var orderitemtotalpvs = new List<OrderItemTotalPV>();
for(int i = ; i < productskus.Count; i++)
{
var orderitem = 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);
return this;
}

QQ讨论群:309287205

DDD实战进阶视频请关注微信公众号:DDD实战进阶第一波(十三):开发一般业务的大健康行业直销系统(订单上下文领域逻辑)-LMLPHP

04-21 11:40