领域驱动设计DDD实战进阶第一波(十四):开发一般业务的大健康行业直销系统(订单上下文应用服务用例与接口)...
上一篇文章我們主要講了訂單上下文的領(lǐng)域邏輯,在領(lǐng)域邏輯中完成了訂單項(xiàng)的計(jì)算邏輯、訂單的計(jì)算邏輯以及如何生成相應(yīng)的實(shí)體code,這篇文章我們通過 在應(yīng)用服務(wù)中實(shí)現(xiàn)一個(gè)下單的用例,來將這些領(lǐng)域邏輯以及倉儲(chǔ)整合起來,完成一個(gè)下單的用例。
先看下單用例主體的代碼:
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;}} } 復(fù)制代碼IOrderRepository倉儲(chǔ)接口主要完成訂單的預(yù)持久化工作,我們來看下它的實(shí)現(xiàn):
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;}} 復(fù)制代碼}
IDealerRepository倉儲(chǔ)接口主要用來下單完成后,扣減對(duì)應(yīng)經(jīng)銷商的電子幣與累加PV,相關(guān)方法實(shí)現(xiàn)代碼如下:
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("電子幣不夠進(jìn)行此操作!");}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;}} 復(fù)制代碼IRepository[]用于訂單與經(jīng)銷商兩個(gè)的數(shù)據(jù)訪問倉儲(chǔ),完成真正的持久化,在第一部分主體代碼中注釋掉的using (var tansaction = new TransactionScope())與 tansaction.Complete();是因?yàn)樵?net core 2.0版本中,不支持多個(gè)數(shù)據(jù)訪問上下文的事務(wù),在.net core 2.1版本中可以使用,這樣就完成了訂單數(shù)據(jù)與經(jīng)銷商 數(shù)據(jù)的事務(wù)一致性。
最后我們通過webapi完成對(duì)應(yīng)用服務(wù)的調(diào)用,實(shí)現(xiàn)代碼如下。
[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;} 復(fù)制代碼因?yàn)檫@里只是做演示,具體接口與實(shí)現(xiàn)沒有通過依賴注入框架注入,這部分內(nèi)容可以自己去實(shí)現(xiàn)。
DDD實(shí)戰(zhàn)進(jìn)階視頻請(qǐng)關(guān)注微信公眾號(hào):MSSHCJ
轉(zhuǎn)載于:https://juejin.im/post/5b7bc335e51d4538a751dcb7
總結(jié)
以上是生活随笔為你收集整理的领域驱动设计DDD实战进阶第一波(十四):开发一般业务的大健康行业直销系统(订单上下文应用服务用例与接口)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信小程序scroll-wiew 横向滚
- 下一篇: windows远程连接报错--“发生身份