zoukankan      html  css  js  c++  java
  • DDD领域模型企业级系统Unity(五)

    添加程序集:

    写一个接口:

      public interface IPlayer
        {
            void Play();
        }
    

     两个实现类:

         public class NewPlay : IPlayer
            {
                public void Play()
                {
                    MessageBox.Show("NewPlay");
                }
            }
    
      public void Play()
            {
                
                MessageBox.Show("OldPlay");
            }
    

     ServiceLocator类:

      public class ServiceLocator
        {
            IUnityContainer container = new UnityContainer();
            public ServiceLocator()
            {            
                container.RegisterType<IPlayer, NewPlay>("new");
                container.RegisterType<IPlayer, OldPlay>("old");
            }
    
            public IUnityContainer GetContainer()
            {
                return container;
            }
        }
    

     调用:获取一个和全部

      ServiceLocator servicelocator =
                    new ServiceLocator();
                var iplay = servicelocator.GetContainer().Resolve<IPlayer>("new");
                var iplays = servicelocator.GetContainer().ResolveAll<IPlayer>();
                foreach(var iplay in iplays)
                {
                    iplay.Play();
                }
    

     构造函数的调用:

      [InjectionConstructor]
            public OldPlay()
            {
                MessageBox.Show("构造函数被执行");
            }
    
     var iplay = servicelocator.GetContainer().Resolve<IPlayer>("old");
    

    属性注入:

    写一个类:

     public class Person
        {
            public string Name { get { return "孙"; } }
            public int Age { get { return 32; } }
        }
    
    [Dependency]
            public Person person { get; set; }
    

     自动实例化

     public void Play()
                {
                    MessageBox.Show("OldPlay "+person.Name);
                }
    

    方法调用注入:

      public Person person { get; set; }
    
    public void Play()
                {
                   
                    MessageBox.Show("OldPlay");
                }
    
            [InjectionMethod]
            public void Auto(Person person)
            {
                MessageBox.Show("注入方法 "+person.Name);
            }
    

     依赖注入的原理:

    添加ProductRepository的仓储:(给聚合根建立仓储)

     public class ProductRepository:EFRepository<Product>
        {
    
        }
    

     添加SalesOrderRepository的仓储:

     public class SalesOrderRepository:EFRepository<SalesOrder>
        {
        }
    

     添加ProductRepository的仓储:

     public class ProductRepository:EFRepository<Product>
        {
    
        }
    

    添加CustomerRepository的仓储:

     public class CustomerRepository:EFRepository<Customer>
        {
        }
    

    定义一个实现Product领域逻辑的部分类(继承聚合根):

    public partial class Product:AggreateRoot
        {
            //定义仓储的接口
            private IRepository<Product> irepository;
            //由调用方指定具体仓储的实现
            public Product(IRepository<Product> irepository)
            {
                this.irepository = irepository;
            }
            //默认构造函数
            public Product()
            { }
    
            /// <summary>
            /// 实现自身聚合根的职责
            /// </summary>
            /// <param name="name"></param>
            /// <param name="color"></param>
            /// <param name="size"></param>
            /// <param name="count"></param>
            /// <param name="unitprice"></param>
            /// <param name="categoryname"></param>
            /// <param name="description"></param>
            public void CreateProduct(string name,string color,string size,int count,
                decimal unitprice,string categoryname,string description)
            {
                var product = new Product();
                product.Id = base.Id; //聚合根的ID
                product.ProductName = name;
                product.Color = color;
                product.Size = size;
                product.Count = count;
                product.UnitPrice = unitprice;
                //聚合根下面的实体和值对象
                var productcategory = new ProductCategory(categoryname,description);
                product.ProductCategory = productcategory;
                //添加到仓储中把对象维护起来
                irepository.Create(product);
    
            }
            /// <summary>
            /// 减少库存的责
            /// </summary>
            /// <param name="p"></param>
            /// <param name="amount"></param>
            /// <param name="irepository"></param>
            public void ModifyCount(Product p,int amount,IRepository<Product> irepository)
            {
                p.Count = this.Count - amount;
                irepository.Update(p);
            }
          
            public Product GetProducyByName(string productname)
            {
                return irepository.GetByCondition(p => p.ProductName == productname)
                    .FirstOrDefault();
            }
        }
    

    定义一个值对象:(负责维护自己的领域逻辑和状态信息)

    public abstract class ValueObject : IValueObject
        {
            public Guid Id
            {
                get
                {
                    var id = Guid.NewGuid();
                    return id;
                }
            }       
        }

     public interface IValueObject
        {
            Guid Id { get; }
        }

     产品类别的逻辑:

     public partial class ProductCategory:ValueObject
        {
            public ProductCategory(string categoryname,string description)
            {
                this.Id = base.Id;
                this.CategoryName = categoryname;
                this.Description = description;
            }
        }
    

     地址的值对象:

        /// <summary>
        /// 值对象
        /// </summary>
        public partial class Address:ValueObject
        {
            public Address(string state,string city,string street)
            {
                this.State = state;
                this.City = city;
                this.Street = street;
            }
        }
    

     定义一个实现Customer领域逻辑的部分类(继承聚合根):

      public partial class Customer:AggreateRoot
        {
            //定义仓储接口
            private IRepository<Customer> irepository;
            //构造函数
            public Customer(IRepository<Customer> irepository)
            {
                this.irepository = irepository;
            }
           
            public void CreateCustomer(string name,string mobile,string state,string city,
                string street)
            {
                Customer customer = new Customer();
                customer.Id = base.Id;
                customer.Name = name;
                customer.Mobile = mobile;
                addcustomeraddress(customer, state, city, street);
                irepository.Create(customer);
            }
          
            /// <summary>
            /// 添加地址
            /// </summary>
            /// <param name="customer"></param>
            /// <param name="state"></param>
            /// <param name="city"></param>
            /// <param name="street"></param>
            private void addcustomeraddress(Customer customer,string state,string city,string street)
            {
                //值对象
                var address = new Address(state, city, street);
                //添加地址
                customer.Address.Add(address);
            }
    
            public void AddCustomerOtherAddress(Customer customer,string state,string city,
                string street)
            {
                addcustomeraddress(customer, state, city, street);
                irepository.Update(customer);
            }
    
            public Customer GetCustomerByName(string name)
            {
                return irepository.GetByCondition(p => p.Name == name).FirstOrDefault();
            }
        }
    

    单元测试(添加引用):

    单元测试:

     EFRepositoryContext context =
                new EFRepositoryContext();
            [TestMethod]
            public void CreateProduct()
            {
                //Product product = new Product(new ProductRepository());
                ProductAppService product = new ProductAppService();          
                product.CreateProduct("P1", "Red", "Small", 100, 55, "C1", "T恤类产品");
                product.CreateProduct("P2", "Green", "Big", 200, 40, "C2", "运动类产品");
                context.Commit();
                Assert.IsNotNull(product.GetProductByName("P1"));          
    
            }
    
            [TestMethod]
            public void CreateCustomer()
            {
                Customer customer = new Customer(new CustomerRepository());
                customer.CreateCustomer("sun", "13458629365", "sanxi", "sanxi", "sanxi");
                context.Commit();
                Assert.IsNotNull(customer.GetCustomerByName("sun"));
            }
    
  • 相关阅读:
    [SDOI2008]Sue的小球
    【洛谷P2611】小蓝的好友
    【YbtOJ#20051】简单游走
    【洛谷P3338】力
    【洛谷P3803】【模板】多项式乘法(FFT)
    【YbtOJ#10053】合影队形
    【YbtOJ#20056】树上取模
    【洛谷P1919】【模板】A*B Problem升级版
    初赛练习总结
    【洛谷P3723】礼物
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/6582527.html
Copyright © 2011-2022 走看看