DDD简明入门之道 - 开篇 (2)

在上面的Order类中,我们给它添加了一系列业务相关的行为(方法),使得其不再象普通三层里的模型只是一个数据容器,而且整个类的设计也更加的面向对象。

public static Order Create(string orderNo, Address address, SaleSkuInfo[] skus)

==Create()方法用来创建新订单,订单的创建是一个复杂的装配过程,这个方法可以封装这些复杂过程,从而降低调用端的调用复杂度。==

public void AddLine(int skuId, int qty)

==AddLine()方法用于将用户购买的商品添加到订单中,该方法中用户只需要传递购买的商品Id和购买数量即可。至于商品的具体信息,比如名称、规格、价格等信息,我们将会在方法中调用产品接口实时去查询。这里涉及到和产品系统的交互,我们定义了一个ServiceProxy类,专门用来封装调用其他系统的交互细节。==

public void CalculateFee()

==CalculateFee()方法用于计算订单的各种费用,如商品总价、运费、优惠等。==

public void Pay(decimal money)

==Pay()方法用于接收交易系统在用户支付完毕后的调用,因为在上文中我们说到订单系统和交易系统是两个单独的系统,他们是通过webapi接口调用进行交互的。订单系统如何知道某个订单支付了多少钱,就得依赖于交易系统的调用传递交易数据了,因为订单系统本身不负责处理用户的交易。==

/// <summary> /// 订单明细 /// </summary> public class OrderLine { public OrderLine() { } public OrderLine(int skuId, string skuName, string spec, int qty, decimal cost, decimal price) : this() { this.SkuId = skuId; this.SkuName = skuName; this.Spec = spec; this.Qty = qty; this.Cost = cost; this.Price = price; } /// <summary> /// Id /// </summary> public int Id { get; set; } /// <summary> /// 商品Id /// </summary> public int SkuId { get; set; } /// <summary> /// 商品名称 /// </summary> public string SkuName { get; set; } /// <summary> /// 商品规格 /// </summary> public string Spec { get; set; } /// <summary> /// 购买数量 /// </summary> public int Qty { get; set; } /// <summary> /// 成本价 /// </summary> public decimal Cost { get; set; } /// <summary> /// 售价 /// </summary> public decimal Price { get; set; } /// <summary> /// 小计 /// </summary> public decimal Total { get; set; } /// <summary> /// 小计金额计算 /// </summary> /// <returns></returns> public decimal CalculateTotal() { this.Total = Qty * Price; return this.Total; } } /// <summary> /// 服务代理 /// </summary> public class ServiceProxy { public static IProductServiceProxy ProductService { get { return new ProductServiceProxy(); } } public static IShipmentServiceProxy ShipmentServiceProxy { get { return new ShipmentServiceProxy(); } } } /// <summary> /// 产品服务代理接口 /// </summary> public class ProductServiceProxy : IProductServiceProxy { public GetProductResponse GetProduct(GetProductRequest request) { //todo zhangsan 这里先硬编码数据进行模拟调用,后期需要调用产品系统Api接口获取数据 if (request.SkuId == 1138) { return new GetProductResponse() { SkuId = 1138, SkuName = "苹果8", Spec = "128G 金色", Cost = 5000m, Price = 6500m }; } if (request.SkuId ==1139) { return new GetProductResponse() { SkuId = 1139, SkuName = "小米充电宝", Spec = "10000MA 白色", Cost = 60m, Price = 100m }; } if (request.SkuId == 1140) { return new GetProductResponse() { SkuId = 1140, SkuName = "怡宝瓶装矿泉水", Spec = "200ML", Cost = 1.5m, Price = 2m }; } return null; } } 逻辑验证

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpywgp.html