添加程序集:

写一个接口:

  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"));
}

DDD领域模型企业级系统Unity(五)的更多相关文章

  1. DDD领域模型企业级系统(二)

    用户层: 1.请求应用层获取用户显示的信息 2.发送命令给应用层要求执行某个命令 应用层: 对用户界面提供各种应用功能(包括信息获取与命令执行),应用层不包含业务逻辑,业务层是由应用层调用领域层(领域 ...

  2. DDD领域模型企业级系统(一)

    领域模型的基本构造块: 1.实体(Entity):有业务生命周期,使用标识进行跟踪. 2.值对象(Value Object):无业务生命周期,用来描述实体. 3.服务(Service):无状态的行为类 ...

  3. DDD领域模型企业级系统Linq的CRUD(四)

    建造一个Product Module类: ProductDBContextDataContext dbcontext = new ProductDBContextDataContext(); publ ...

  4. DDD领域模型企业级系统(三)

    相关代码: public static void ShowArray() { //数据源 int[] arrayas = new int[] { 1, 2, 3, 4 }; //创建查询 var qu ...

  5. DDD领域模型和充血对象

    DDD领域模型 官方说法 领域驱动设计,它是对面向对象的的分析和设计(OOAD,Object Orient Analysis Design)的一个补充,对技术框架进行了分层规划,同时对每个类进行了策略 ...

  6. Android系统的五种数据存储形式(一)

    Android系统有五种数据存储形式,分别是文件存储.SP存储.数据库存储.contentprovider 内容提供者.网络存储.其中,前四个是本地存储.存储的类型包括简单文本.窗口状态存储.音频视频 ...

  7. 【百度地图API】建立全国银行位置查询系统(五)——如何更改百度地图的信息窗口内容?

    原文:[百度地图API]建立全国银行位置查询系统(五)--如何更改百度地图的信息窗口内容? 摘要: 酷讯.搜房.去哪儿网等大型房产.旅游酒店网站,用的是百度的数据库,却显示了自定义的信息窗口内容,这是 ...

  8. Android系统--输入系统(五)输入系统框架

    Android系统--输入系统(五)输入系统框架 1. Android设备使用场景: 假设一个Android平板,APP功能.系统功能(开机关机.调节音量).外接设备功能(键盘.触摸屏.USB外接键盘 ...

  9. DDD领域模型实现依赖注入(六)

    添加下订单的值对象: public partial class CustomerInfo:ValueObject { /// <summary> /// 下订单的值对象 /// </ ...

随机推荐

  1. npm 切换淘宝镜像几种方式

    淘宝 npm 地址: http://npm.taobao.org/ 如何使用 有很多方法来配置npm的registry地址,下面根据不同情境列出几种比较常用的方法.以淘宝npm镜像举例: 1.临时使用 ...

  2. 一本通1649【例 2】2^k 进制数

    1649:[例 2]2^k 进制数 时间限制: 1000 ms         内存限制: 524288 KB [题目描述] 原题来自:NOIP 2006 提高组 设 r 是个 2k 进制数,并满足以 ...

  3. Java多线程与线程同步

    六.多线程,线程,同步 ①概念: 并行:指两个或多个在时间同一时刻发生(同时发生) 并发:指两个或多个事件在同一时间段内发生 具体概念: 在操作系统中,安装了多个程序,并发指的是在一段时间内宏观上有多 ...

  4. MT【195】三次函数

    (2016年清华大学自主招生暨领军计划试题) 已知$x,y,z\in \mathbf{R}$,满足$x+y+z=1,x^2+y^2+z^2=1$,则下列结论正确的有( ) A.$xyz$的最大值为$0 ...

  5. 学习Spring Boot:(十二)Mybatis 中自定义枚举转换器

    前言 在 Spring Boot 中使用 Mybatis 中遇到了字段为枚举类型,数据库存储的是枚举的值,发现它不能自动装载. 解决 内置枚举转换器 MyBatis内置了两个枚举转换器分别是:org. ...

  6. AtCoder Grand Contest 006

    AtCoder Grand Contest 006 吐槽 这套题要改个名字,叫神仙结论题大赛 A - Prefix and Suffix 翻译 给定两个串,求满足前缀是\(S\),后缀是\(T\),并 ...

  7. stm32 外设使用的配置步骤

    @2018-5-10  使用外设的配置步骤 #1 打开时钟 > 打开外设时钟 > 打开相关GPIO时钟 > 打开DMA时钟  (若需要) #2 关联外设与GPIO > 复位关联 ...

  8. 使用tushare的pandas进行to_sql操作时的No module named 'MySQLdb'错误处理

    先写在前面,用tushare获取财经类数据时,完全没有必要用python3版本 py2功能没差别,但是py3有很多地方需要修改参数才能成功运行,无端造成时间的浪费 下面进入正题,这个问题困扰了我一个下 ...

  9. Python字符串,整型,浮点数相互转化

    Python字符串,整型,浮点数相互转化 觉得有用的话,欢迎一起讨论相互学习~Follow Me int(str) 函数将符合整数的规范的字符串转换成int型 float(str) 函数将符合浮点数的 ...

  10. MySql与对应的Java的时间类型

    MySql的时间类型有          Java中与之对应的时间类型date                                           java.sql.Date Date ...