添加下订单的值对象:
public partial class CustomerInfo:ValueObject
{
///
/// 下订单的值对象
///
/// Customer值对象
/// Address值对象
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 irepository;
//定义构造函数
public SalesOrder(IRepository irepository)
{
this.irepository = irepository;
}
///
/// 创建订单
///
/// 产品列表
/// 客户列表
/// 产品数量
/// 地址
///
///
public void CreateOrder(List products,Customer customer,List 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
跨聚合的事务处理一般通过领域服务来做。(DomainService)主要是协调的作用
public class SalesOrderService:ISalesOrderService
{
private IRepository irepositoryproduct;
private IRepository irepositorycustomer;
private IRepository irepositorysalesorder;
[InjectionConstructor]
public SalesOrderService(IRepository irepositoryproduct
,IRepository irepositorycustomer
,IRepository irepositorysalesorder)
{
this.irepositoryproduct = irepositoryproduct;
this.irepositorycustomer = irepositorycustomer;
this.irepositorysalesorder = irepositorysalesorder;
}
///
/// 领域服务面向界面的内容
///
/// 产品名字列表
/// 产品名字对应的数量
/// 客户名
/// 地址
///
///
public void CreateSalesOrder(List productnames,List amounts,
string customername,string state,string city,string street)
{
//生成Product列表对象
var listproduct = new List();
//得到具体的产品减少库存
for(int i=0;i
添加界面和核心业务层保护的作用:(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()
{
//解析泛型
return container.Resolve();
}
//返回具体实现类的Object
public T GetService(object overridedarguments)
{
var overrides = GetParameterOverride(overridedarguments);
return container.Resolve(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 GetParameterOverride(object overridearguments)
{
var overrides = new List();
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 productnames, List 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 productrepository = ServiecLocator.Instance.GetService(typeof(IRepository))
as IRepository;
IRepository customerrepository = ServiecLocator.Instance.GetService(typeof(IRepository))
as IRepository;
IRepository salesorderrepository = ServiecLocator.Instance.GetService(typeof(IRepository))
as IRepository;
ISalesOrderService salesorderservice = ServiecLocator.Instance.GetService(typeof(ISalesOrderService)
, new ParameterOverrides { { "irepositoryproduct", "productrepository" }
, {"irepositorycustomer","customerrepository" },
{ "irepositorysalesorder","salesorderrepository" } })
as ISalesOrderService;
public void CreateSalesOrder(List productnames,
List amounts,string customername,string state,string city,string street)
{
//var salesorderservice = new SalesOrderService
// (new EFRepository(), new EFRepository()
// , new EFRepository());
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节点)
具体映射机制: