领域驱动设计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 ResultEntity<bool> CreateOrder(OrderDTO orderdto){var orderid = Guid.NewGuid();Orders order = new Orders();var productskus = new List<ProductSKU>();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>(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 ResultEntity<bool> CreateOrder([FromBody] OrderDTO orderdto){var result = new ResultEntity<bool>();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;} 復制代碼因為這里只是做演示,具體接口與實現沒有通過依賴注入框架注入,這部分內容可以自己去實現。
DDD實戰進階視頻請關注微信公眾號:MSSHCJ
轉載于:https://juejin.im/post/5b7bc335e51d4538a751dcb7
總結
以上是生活随笔為你收集整理的领域驱动设计DDD实战进阶第一波(十四):开发一般业务的大健康行业直销系统(订单上下文应用服务用例与接口)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序scroll-wiew 横向滚
- 下一篇: webpack-dev-server 和