zoukankan      html  css  js  c++  java
  • 领域驱动设计案例之实现业务3

    这一部分主要介绍如何实现下订单的业务,下订单的业务主要涉及到SalesOrder,OrderItem,CustomerInfo与ProductInfo几个领域对象

      public partial class ProductInfo:ValueObject
        {
            public ProductInfo(Product product)
            {
                this.Id = base.Id;
                this.Name = product.ProductName;
                this.UnitPrice = product.UnitPrice;
            }
        }
     public partial class CustomerInfo:ValueObject
        {
            public CustomerInfo(Customer customer,Address address)
            {
                this.Id = base.Id;
                this.Name = customer.Name;
                this.Mobile = customer.Mobile;
                this.State = address.State;
                this.City = address.City;
                this.Street = address.Street;
            }
        }
    public partial class OrderItem:Entity
        {
            public OrderItem(Product product,int amount)
            {
                this.Id = base.Id;
                this.Amount = amount;
                this.LineTotal = product.UnitPrice * Amount;
            }
        }
     public partial class SalesOrder:AggreateRoot
        {
            private IRepository<SalesOrder> irepository;
            public SalesOrder(IRepository<SalesOrder> irepository)
            {
                this.irepository = irepository;
            }
            public void CreateOrder(List<Product> products,Customer customer,List<int> amounts,
                string state,string city,string street)
            {
                SalesOrder salesorder = new SalesOrder();
                salesorder.Id = base.Id;
                salesorder.DateTime = DateTime.Now;
                salesorder.CustomerInfo =
                    new CustomerInfo(customer, new Address(state, city, street));
                for(int i=0;i<products.Count;i++)
                {
                    var orderitem = new OrderItem(products[i], amounts[i]);
                    orderitem.ProductInfo = new ProductInfo(products[i]);
                    salesorder.OrderItem.Add(orderitem);
                    salesorder.TotalPrice = salesorder.TotalPrice + orderitem.LineTotal;
                }
    
                irepository.Create(salesorder);
            }       
        }

    在下订单时,因为会涉及到多个聚合根的协作,所以在DDD.Domain中引入一个领域服务SalesOrderService来进行协调,比如订单项添加成功后,产品的库存
    也要相应减少。

     public class SalesOrderService
        {
            private IRepository<Product> irepositoryproduct;
            private IRepository<Customer> irepositorycustomer;
            private IRepository<SalesOrder> irepositorysalesorder;
            public SalesOrderService(IRepository<Product> irepositoryproduct
                ,IRepository<Customer> irepositorycustomer
                ,IRepository<SalesOrder> irepositorysalesorder)
            {
                this.irepositoryproduct = irepositoryproduct;
                this.irepositorycustomer = irepositorycustomer;
                this.irepositorysalesorder = irepositorysalesorder;
            }
            public void CreateSalesOrder(List<string> productnames,List<int> amounts,
                string customername,string state,string city,string street)
            {
                var listproduct = new List<Product>();
                for(int i=0;i<productnames.Count;i++)
                {
                    var product =
                        new Product(irepositoryproduct).GetProducyByName(productnames[i]);
                    product.ModifyCount(product, amounts[i], irepositoryproduct);
    
                    listproduct.Add(product);
                }
                var customer = new Customer(irepositorycustomer).GetCustomerByName(customername);
                var salesorder = new SalesOrder(irepositorysalesorder);
    
                salesorder.CreateOrder(listproduct, customer, amounts, state, city, street);
            }
        }


    最后在DDD.Application中建立一个应用层服务,实现事务提交(这里只演示SalesOrder应用服务,产品与客户的应用服务提交情况类似)

      public class SalesOrderAppService
        {
            EFRepositoryContext context = new EFRepositoryContext();
    
            public void CreateSalesOrder(List<string> productnames,
                List<int> amounts,string customername,string state,string city,string street)
            {
                var salesorderservice = new SalesOrderService
                    (new ProductRepository(), new CustomerRepository(), new SalesOrderRepository());
                salesorderservice.CreateSalesOrder(productnames, amounts, customername,
                    state, city, street);
    
                context.Commit();
            }
        }

     欢迎加入QQ讨论群:309287205

  • 相关阅读:
    调试D2JS
    PG 中 JSON 字段的应用
    面试----
    机器学习面试题
    闭包和装饰器
    scss-混合@mixin @include @function
    scss基本使用及操作函数
    常用的scss函数(mixin)
    二叉搜索树基本操作实现
    判断一棵树是否是二叉搜索树
  • 原文地址:https://www.cnblogs.com/malaoko/p/5050134.html
Copyright © 2011-2022 走看看