添加下订单的值对象:

 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>

DDD领域模型实现依赖注入(六)的更多相关文章

  1. IoC容器Autofac正篇之依赖注入(六)

    依赖注入,这个专业词我们可以分为两个部分来理解: 依赖,也就是UML中描述事物之间关系的依赖关系,依赖关系描述了事物A在某些情况下会使用到事物B,事物B的变化会影响到事物A: 注入,医生通过针头将药物 ...

  2. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之九 || 依赖注入IoC学习 + AOP界面编程初探

    更新 1.如果看不懂本文,或者比较困难,先别着急问问题,我单写了一个关于依赖注入的小Demo,可以下载看看,多思考思考注入的原理: https://github.com/anjoy8/BlogArti ...

  3. Z从壹开始前后端分离【 .NET Core2.2/3.0 +Vue2.0 】框架之九 || 依赖注入IoC学习 + AOP界面编程初探

    本文梯子 本文3.0版本文章 更新 代码已上传Github+Gitee,文末有地址 零.今天完成的绿色部分 一.依赖注入的理解和思考 二.常见的IoC框架有哪些 1.Autofac+原生 2.三种注入 ...

  4. 基于DDD的.NET开发框架 - ABP依赖注入

    返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...

  5. MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息

    MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...

  6. Lind.DDD.IoC依赖注入与面向方面的实现

    回到目录 IoC是解耦的灵魂,很难想像一个框架中没有IoC会变成什么样子,Lind.DDD里的IoC是通过Unity实现的,由依赖注入(unity)和方法拦截组成(Interception),依赖注入 ...

  7. Web API(六):使用Autofac实现依赖注入

    在这一篇文章将会讲解如何在Web API2中使用Autofac实现依赖注入. 一.创建实体类库 1.创建单独实体类 创建DI.Entity类库,用来存放所有的实体类,新建用户实体类,其结构如下: us ...

  8. SpringBoot启动流程分析(六):IoC容器依赖注入

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  9. ABP源码分析六:依赖注入的实现

    ABP的依赖注入的实现有一个本质两个途径:1.本质上是依赖于Castle这个老牌依赖注入的框架.2.一种实现途径是通过实现IConventionalDependencyRegistrar的实例定义注入 ...

随机推荐

  1. centos6.5环境下安装zk

    第一步:先下载安装包,解压. 第二步:进去根目录,创建data文件夹  mkdir  data 第三步:进去conf文件夹,修改  zoo_sample.cfg    的名字   mv zoo_sam ...

  2. javascript 转换大小写字母

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. CodeBlock 快捷键大全

     一款开源的C/C++ IDE(集成开发环境),基于wxWidgets GUI体系,跨平台支持. 编辑器 快捷键 功能 Ctrl+Z 恢复上一次操作 Ctrl+Shift+Z 重复上一次操作 F1 ...

  4. readn.c

    #include <errno.h> #include <unistd.h> ssize_t readn(int fd, void *vptr, size_t n) { siz ...

  5. Linux 环境变量问题

    环境变量延伸: /etc/profile, /etc/bashrc, .bash_profile和.bashrc的差别 用户在登陆Linux操作系统的时候,"/etc/profile&quo ...

  6. POJ 1811 Prime Test (Rabin-Miller强伪素数测试 和Pollard-rho 因数分解)

    题目链接 Description Given a big integer number, you are required to find out whether it's a prime numbe ...

  7. Linux命令行与shell脚本编程大全.第3版(文字版) 超清文字-非扫描版 [免积分、免登录]

    此处免费下载,无需账号,无需登录,无需积分.收集自互联网,侵权通知删除. 点击下载:Linux命令行与shell脚本编程大全.第3版 (大小:约22M)

  8. GCC的符号可见性——解决多个库同名符号冲突问题

    引用自:https://github.com/wwbmmm/blog/wiki/gcc_visibility 问题 最近项目遇到一些问题,场景如下 主程序依赖了两个库libA的funcA函数和libB ...

  9. WGAN源码解读

    WassersteinGAN源码 作者的代码包括两部分:models包下包含dcgan.py和mlp.py, 这两个py文件是两种不同的网络结构,在dcgan.py中判别器和生成器都含有卷积网络,而m ...

  10. 简述JavaScript作用域与作用域链

    关于变量作用域的知识,相信学习JavaScript的朋友们一定早已经接触过,这里简单列举: JavaScript中变量是以对象属性的形式存在的:全局变量是全局对象的属性:局部变量是声明上下文对象的属性 ...