这一部分主要介绍如何实现下订单的业务,下订单的业务主要涉及到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