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>
DDD领域模型实现依赖注入(六)的更多相关文章
- IoC容器Autofac正篇之依赖注入(六)
依赖注入,这个专业词我们可以分为两个部分来理解: 依赖,也就是UML中描述事物之间关系的依赖关系,依赖关系描述了事物A在某些情况下会使用到事物B,事物B的变化会影响到事物A: 注入,医生通过针头将药物 ...
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之九 || 依赖注入IoC学习 + AOP界面编程初探
更新 1.如果看不懂本文,或者比较困难,先别着急问问题,我单写了一个关于依赖注入的小Demo,可以下载看看,多思考思考注入的原理: https://github.com/anjoy8/BlogArti ...
- Z从壹开始前后端分离【 .NET Core2.2/3.0 +Vue2.0 】框架之九 || 依赖注入IoC学习 + AOP界面编程初探
本文梯子 本文3.0版本文章 更新 代码已上传Github+Gitee,文末有地址 零.今天完成的绿色部分 一.依赖注入的理解和思考 二.常见的IoC框架有哪些 1.Autofac+原生 2.三种注入 ...
- 基于DDD的.NET开发框架 - ABP依赖注入
返回ABP系列 ABP是“ASP.NET Boilerplate Project (ASP.NET样板项目)”的简称. ASP.NET Boilerplate是一个用最佳实践和流行技术开发现代WEB应 ...
- MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息
MVVM模式解析和在WPF中的实现(六) 用依赖注入的方式配置ViewModel并注册消息 系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二 ...
- Lind.DDD.IoC依赖注入与面向方面的实现
回到目录 IoC是解耦的灵魂,很难想像一个框架中没有IoC会变成什么样子,Lind.DDD里的IoC是通过Unity实现的,由依赖注入(unity)和方法拦截组成(Interception),依赖注入 ...
- Web API(六):使用Autofac实现依赖注入
在这一篇文章将会讲解如何在Web API2中使用Autofac实现依赖注入. 一.创建实体类库 1.创建单独实体类 创建DI.Entity类库,用来存放所有的实体类,新建用户实体类,其结构如下: us ...
- SpringBoot启动流程分析(六):IoC容器依赖注入
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- ABP源码分析六:依赖注入的实现
ABP的依赖注入的实现有一个本质两个途径:1.本质上是依赖于Castle这个老牌依赖注入的框架.2.一种实现途径是通过实现IConventionalDependencyRegistrar的实例定义注入 ...
随机推荐
- JAVA记录-SpringMVC scope属性的两种模式
singleton作用域:当把一个Bean定义设置为singleton作用域是,Spring IoC容器中只会存在一个共享的Bean实例,并且所有对Bean的请求,只要id与该Bean定义相匹配,则只 ...
- Linux下解压缩
1)对于.tar结尾的文件 tar -xf all.tar 2)对于.gz结尾的文件 gzip -d all.gz gunzip all.gz 3)对于.tgz或.tar.gz结尾的文件 tar -x ...
- Linux 开机启动图形界面,shell界面
查看当前启动模式 # systemctl get-default 更改模式命令: systemctl set-default graphical.target由命令行模式更改为图形界面模式 syste ...
- Java EE之Hibernate异常总结【2】Field 'id' doesn't have a default value
Field 'id' doesn't have a default value问题解决方法 因为assigned是指主键是由人工分配的,而native则指主键值由库自动给出. <generato ...
- JavaScript之获取表格目标数据(TableDom.getTableData())
[声明: 1.博文原创 未经同意转载必究,欢迎相互交流] [声明: 2.博主未知情况下转载,需显著处注明博文来源] [声明: 3.谢谢尊重劳动成果,谢谢理解与配合~] 一.背景 在生产过程和生活 ...
- oracle锁表
一.锁表的处理 Oracle锁表比较简单,查询锁表的session杀掉就可以了. 1.以下几个为相关表 SELECT * FROM V$LOCK; SELECT * FROM V$SQLAREA; S ...
- [转]xargs命令详解,xargs与管道的区别
为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...
- DSO windowed optimization 代码 (3)
4 Schur Complement 部分信息计算 参考<DSO windowed optimization 公式>,Schur Complement 部分指 Hsc(\(H_{X\rho ...
- 11、Logback日志框架介绍和SpringBoot整合实战 2节课
1.新日志框架LogBack介绍 简介:日志介绍和新日志框架Logback讲解 1.常用处理java的日志组件 slf4j,log4j,logback,common-logging 等 ...
- ROS 多台计算机联网控制机器人
0. 时间同步 sudo apt-get install chrony 1. ubuntu自带的有openssh-client 可以通过如下指令 ssh username@host 来连接同一局域网内 ...