zoukankan      html  css  js  c++  java
  • DDD领域模型实现依赖注入(六)

    添加下订单的值对象:

     public partial class CustomerInfo:ValueObject
        {
            /// <summary>
            /// 下订单的值对象
            /// </summary>
            /// <param name="customer">Customer值对象</param>
            /// <param name="address">Address值对象</param>
            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 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;
            }
            /// <summary>
            /// 创建订单
            /// </summary>
            /// <param name="products">产品列表</param>
            /// <param name="customer">客户列表</param>
            /// <param name="amounts">产品数量</param>
            /// <param name="state">地址</param>
            /// <param name="city"></param>
            /// <param name="street"></param>
            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);
            }       
        }
    

     跨聚合的事务处理一般通过领域服务来做。(DomainService)主要是协调的作用

     public class SalesOrderService:ISalesOrderService
        {
            private IRepository<Product> irepositoryproduct;
            private IRepository<Customer> irepositorycustomer;
            private IRepository<SalesOrder> irepositorysalesorder;
    
            [InjectionConstructor]
            public SalesOrderService(IRepository<Product> irepositoryproduct
                ,IRepository<Customer> irepositorycustomer
                ,IRepository<SalesOrder> irepositorysalesorder)
            {
                this.irepositoryproduct = irepositoryproduct;
                this.irepositorycustomer = irepositorycustomer;
                this.irepositorysalesorder = irepositorysalesorder;
            }
            /// <summary>
            /// 领域服务面向界面的内容
            /// </summary>
            /// <param name="productnames">产品名字列表</param>
            /// <param name="amounts">产品名字对应的数量</param>
            /// <param name="customername">客户名</param>
            /// <param name="state">地址</param>
            /// <param name="city"></param>
            /// <param name="street"></param>
            public void CreateSalesOrder(List<string> productnames,List<int> amounts,
                string customername,string state,string city,string street)
            {
                //生成Product列表对象
                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)

    添加引用:

    添加基础服务实现的机制:(DDD.Infrastructure)----通过Unity实现

     public class ServiecLocator
        {
            //定义Unity的容器
            private readonly IUnityContainer container;
         
            //构造函数
            public ServiecLocator()
            {
                //定义配置节点
                var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
                container = new UnityContainer();
                //指定使用节点的信息
                section.Configure(container);
            }
            //静态属性
            public static ServiecLocator Instance
            {
                get { return new ServiecLocator(); }
            }
            //得到接口对应的具体服务
            public T GetService<T>()
            {
                //解析泛型
                return container.Resolve<T>();
            }
            //返回具体实现类的Object
            public T GetService<T>(object overridedarguments)
            {
                var overrides = GetParameterOverride(overridedarguments);
                return container.Resolve<T>(overrides.ToArray());
            }
           
            public object GetService(Type serviectype)
            {
                return container.Resolve(serviectype);
            }
    
            public object GetService(Type servicetype,object overridedarguments)
            {
                var overrides = GetParameterOverride(overridedarguments);
                return container.Resolve(servicetype,overrides.ToArray());
            }
            //定义构造函数参数的信息
            private IEnumerable<ParameterOverride> GetParameterOverride(object overridearguments)
            {
                var overrides = new List<ParameterOverride>();
                var argumenttype = overridearguments.GetType();
                argumenttype.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance)
                    .ToList()
                    .ForEach(property =>
                    {
                        var propertyvalue = property.GetValue(overridearguments, null);
                        var propertyname = property.Name;
                        overrides.Add(new ParameterOverride(propertyname, propertyvalue));
                     
                    });
    
                return overrides;
            }
        }
    

     创建订单定义的接口:

    //创建订单定义的接口
        public interface ISalesOrderService
        {
            void CreateSalesOrder(List<string> productnames, List<int> amounts,
                string customername, string state, string city, string street); 
        }
    

     Unity的替换:

      public class SalesOrderAppService
        {
            //EFRepositoryContext context = new EFRepositoryContext();
            IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext))
                as IRepositoryContext;
            //定义接口
            IRepository<Product> productrepository = ServiecLocator.Instance.GetService(typeof(IRepository<Product>))
                as IRepository<Product>;
            IRepository<Customer> customerrepository = ServiecLocator.Instance.GetService(typeof(IRepository<Customer>))
        as IRepository<Customer>;
            IRepository<SalesOrder> salesorderrepository = ServiecLocator.Instance.GetService(typeof(IRepository<SalesOrder>))
        as IRepository<SalesOrder>;
    
            ISalesOrderService salesorderservice = ServiecLocator.Instance.GetService(typeof(ISalesOrderService)
                , new ParameterOverrides { { "irepositoryproduct", "productrepository" }
                    , {"irepositorycustomer","customerrepository" },
                    { "irepositorysalesorder","salesorderrepository" } })
        as ISalesOrderService;
    
            public void CreateSalesOrder(List<string> productnames,
                List<int> amounts,string customername,string state,string city,string street)
            {
                //var salesorderservice = new SalesOrderService
                //    (new EFRepository<Product>(), new EFRepository<Customer>()
                //    , new EFRepository<SalesOrder>());
                salesorderservice.CreateSalesOrder(productnames, amounts, customername,
                    state, city, street);
    
                context.Commit();
            }
        }
    

     产品的替换:

       IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext))
               as IRepositoryContext;
            Product product = new Product(new ProductRepository());
    

     配置文件:(指定unity节点)

     <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
    

     具体映射机制:

     <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
        <sectionExtension type="Microsoft.Practices.Unity.InterceptionExtension.Configuration.InterceptionConfigurationExtension,Microsoft.Practices.Unity.Interception.Configuration" />
        <container>
          <extension type="Interception"/>
          <!--类型-->
          <register type="DDD.Domain.DomainService.ISalesOrderService,DDD.Domain" mapTo="DDD.Domain.DomainService.SalesOrderService,DDD.Domain">
          </register>
          <!--上下文-->
          <register type="DDD.Domain.Repository.IRepositoryContext,DDD.Domain" mapTo="DDD.Repository.EFRepositoryContext,DDD.Repository">
          </register>
          <!--仓储 `泛型的写法 -->
          <register type="DDD.Domain.Repository.IRepository`1,DDD.Domain" mapTo="DDD.Repository.EFRepository`1,DDD.Repository">
          </register>
        </container>
      </unity>
  • 相关阅读:
    [MSDN] How to Debug a Release Build
    抽象成员 虚方法
    强制类型转换符 和 as 运算符
    一份超长的MySQL学习笔记
    Java反射基础
    c3p0config.xml
    一个JDBC封装工具类
    Spring5——IOC操作Bean管理(基于xml文件)
    Android游戏开发大全
    移除项目里的所有.svn命令
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/7857433.html
Copyright © 2011-2022 走看看