DDD实战进阶第一波(十):开发一般业务的大健康行业直销系统(订单上下文领域逻辑)
前一篇文章主要讲了订单上下文的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 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 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 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 = new List();
var orderitemtotalprices = new List();
var orderitemtotalpvs = new List();
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;
}
DDD实战进阶第一波(十):开发一般业务的大健康行业直销系统(订单上下文应用服务用例与接口)
上一篇文章我们主要讲了订单上下文的领域逻辑,在领域逻辑中完成了订单项的计算逻辑、订单的计算逻辑以及如何生成相应的实体code,这篇文章我们通过在应用服务中实现一个下单的用例,来将这些领域逻辑以及仓储整合起来,完成一个下单的用例。
先看下单用例主体的代码:
public class CreateOrderUseCase:BaseAppSrv { private readonly IOrderRepository iorderrepository; private readonly IDealerRepository idealerrepository; private readonly IRepository[] irepositories; public CreateOrderUseCase(IOrderRepository iorderrepository,IDealerRepository idealerrepository, params IRepository[] irepositories) { this.iorderrepository = iorderrepository; this.idealerrepository = idealerrepository; this.irepositories = irepositories; } public ResultEntityCreateOrder(OrderDTO orderdto) { var orderid = Guid.NewGuid(); Orders order = new Orders(); var productskus = new List (); for(int i = 0; i < orderdto.ProductSPUNames.Count; i++) { var productsku = new ProductSKU(); productsku.ProductSPUName = orderdto.ProductSPUNames[i]; productsku.DealerPrice = orderdto.ProductDealerPrices[i]; productsku.PV = orderdto.ProductPVS[i]; productsku.Id = orderdto.ProductSKUIds[i]; productsku.Spec = orderdto.ProductSepcs[i]; productskus.Add(productsku); } var contact = new Contact(); contact.ContactName = orderdto.ContactName; contact.ContactTel = orderdto.ContactTel; contact.Province = orderdto.Privence; contact.City = orderdto.City; contact.Zero = orderdto.Zero; contact.Street = orderdto.Street; var orders = order.CreateOrders(orderid, orderdto.DealerId, productskus, orderdto.Counts, contact); try { //using (var tansaction = new TransactionScope()) //{ using (irepositories[1]) { idealerrepository.SubParentEleMoney(orderdto.DealerId, order.OrderTotalPrice.TotalPrice); idealerrepository.AddDealerPV(orderdto.DealerId, order.OrderTotalPV.TotalPV); irepositories[1].Commit(); } using (irepositories[0]) { iorderrepository.CreateOrder(orders); irepositories[0].Commit(); } return GetResultEntity(true); //tansaction.Complete(); //} } catch(EleMoneyNotEnoughException error) { throw error; } catch(Exception error) { throw error; } } }
IOrderRepository仓储接口主要完成订单的预持久化工作,我们来看下它的实现:
public class OrderEFCoreRepository : IOrderRepository { private readonly DbContext context; public OrderEFCoreRepository(DbContext context) { this.context = context; } public void CreateOrder(T order) where T:class,IAggregationRoot { var ordercontext = this.context as OrderEFCoreContext; var ordernew = order as Orders; try { ordercontext.Order.Add(ordernew); } catch(Exception error) { throw error; } } }
IDealerRepository仓储接口主要用来下单完成后,扣减对应经销商的电子币与累加PV,相关方法实现代码如下:
public void SubParentEleMoney(Guid parentdealerid, decimal subelemoney) { var dealercontext = this.context as DealerEFCoreContext; var parentdealer = dealercontext.Dealer.Single(p => p.Id == parentdealerid); parentdealer.TotalEleMoney = parentdealer.TotalEleMoney - subelemoney; if (parentdealer.TotalEleMoney < 0) { throw new EleMoneyNotEnoughException("电子币不够进行此操作!"); } try { dealercontext.Entry(parentdealer).State = EntityState.Modified; } catch(Exception error) { throw error; } } public void AddDealerPV(Guid dealerid, decimal orderpv) { var dealercontext = this.context as DealerEFCoreContext; var dealer = dealercontext.Dealer.Single(p => p.Id == dealerid); dealer.TotalPV = dealer.TotalPV +orderpv; try { dealercontext.Entry(dealer).State = EntityState.Modified; } catch (Exception error) { throw error; } }
IRepository[]用于订单与经销商两个的数据访问仓储,完成真正的持久化,在第一部分主体代码中注释掉的using (var tansaction = new TransactionScope())与tansaction.Complete();是因为在.net core 2.0版本中,不支持多个数据访问上下文的事务,在.net core 2.1版本中可以使用,这样就完成了订单数据与经销商数据的事务一致性。
最后我们通过webapi完成对应用服务的调用,实现代码如下。
[HttpPost] [Route("CreateOrder")] public ResultEntityCreateOrder([FromBody] OrderDTO orderdto) { var result = new ResultEntity (); var ordercontext = new OrderEFCoreContext(); var dealercontext = new DealerEFCoreContext(); var irepository = new EFCoreRepository(ordercontext); var irepository1 = new EFCoreRepository(dealercontext); var iorderrepository = new OrderEFCoreRepository(ordercontext); var idealerrepository = new DealerEFCoreRepository(dealercontext); var createorderusecase = new CreateOrderUseCase(iorderrepository, idealerrepository, irepository, irepository1); try { result = createorderusecase.CreateOrder(orderdto); result.Count = 1; result.IsSuccess = true; result.Msg = "下单成功!"; } catch (EleMoneyNotEnoughException error) { result.ErrorCode = 300; result.Msg = error.Message; } catch (Exception error) { result.ErrorCode = 200; result.Msg = error.Message; } return result; }
因为这里只是做演示,具体接口与实现没有通过依赖注入框架注入,这部分内容可以自己去实现。