public void AddItem(BizofferInfo info) { if (info == null) { throw new Exception("发布单信息不能为空"); } if (info.Price <= 0) { throw new Exception("发布单价格不能小于等于0"); } //由于order和orderItem是个聚合关系,order是这个聚合的聚合根,它负责业务规则的不变性和数据的一致性 //下面逻辑体现了以上原则 this.TotalPrice = this.TotalPrice + info.Price;//数据的一致性 var items = this.OrderItems.Where(m => m.BizofferInfo.BizofferId == info.BizofferId).ToList(); //业务的不变性,如果订单项列表中已有该商品,则订单项商品数加一即可 if (items.Count > 0) { items[0].IncrementQuantity(); } else { OrderItem item = new OrderItem(info); this.OrderItems.Add(item); } }
/// <summary> /// 下单 /// </summary> /// <param name="bizofferId"></param> /// <param name="buyerId"></param> /// <param name="buyerName"></param> /// <returns></returns> public Order Execute(string bizofferId, string buyerId, string buyerName) { //调用风控服务,是否是黑名单用户 var IsExistsBlackList = this.securityService.IsExistsBlackList(buyerId); if (IsExistsBlackList) { throw new Exception("该用户是黑名单用户"); //领域层里所有业务检查都抛出异常的方式处理,否则应用服务层要有if判断语句 } //调用发布单上下文获取发布单信息 BizofferInfo bizofferInfo = this.bizofferService.GetBizofferInfoById(bizofferId); //创建订单 var order = new Order(buyerId, buyerName); order.AddItem(bizofferInfo); return(order); }
public OrderItem(BizofferInfo bizofferInfo) { this.BizofferInfo = bizofferInfo; this.Id = Guid.NewGuid().ToString(); }