The previous article focused on the POCO model of order context, where orders and order items have a large number of value objects. This article focuses on these value objects as well as order items and their related domain logic.

ProductSKUs value object domain logic:

The ProductSKUs value object is used in the order item entity, and its information should come from the ProductSKU entity in the product context.

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; }}Copy the code

2.OrderItemTotalPV object domain logic:

The domain logic of the OrderItemTotalPV object is mainly to calculate the total PV value of the order item from the PV and quantity of the ProductSKU purchased by the order item.

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

3.OrderItemTotalPrice object domain logic:

The domain logic of the OrderItemTotalPrice value object basically calculates the total price of the order item from the unit price and quantity of the ProductSKU purchased by the order item.

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

4. Domain logic for OrderItem entities:

The domain logic of OrderItem entity mainly consists of two aspects: one is the Code generation rule of OrderItem; the other is to call the domain logic of the above three value objects contained in OrderItem entity to generate the related value objects.

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; }}Copy the code

5.OrderStreet value object domain logic:

The information for the OrderStreet value object is mainly derived from the contact information identified at the time of product purchase to assign the associated properties to the OrderStreet value object.

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; }}Copy the code

6.OrderTotalPV value object domain logic:

The value of the OrderTotalPV value object is summed by the OrderItemTotalPV value object for each order item.

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; }}Copy the code

7.OrderTotalPrice value object domain logic:

The value of the OrderTotalPrice value object is summed by the OrderItemTotalPrice value object for each order item.

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; }}Copy the code

8.Orders aggregates root domain logic:

The Orders aggregate root is essentially the domain logic that coordinates the OrderItem entity and its three value objects to complete the entire order.

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 = 0; 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;
    }
Copy the code

DDD combat advanced video please pay attention to wechat public number: MSSHCJ