DDD领域模型AutoMapper实现DTO(七)
DTO的应用场景:

定义产品类:
public class Product
{
public string ProductName { get; set; }
public decimal ProductUnitPrice { get; set; }
}
NueGet 添加AutoMapper映射组件。
定义ProductDTO对象:
public class ProductDTO
{
public string ProductName { get; set; }
public decimal ProductUnitPrice { get; set; }
}
定义两个类:
public class Address
{
public string AddressState { get; set; }
public string AddressCity { get; set; }
public string AddressStreet { get; set; }
}
public class Customer
{
public string CustomerName { get; set; }
public string CustomerMobile { get; set; }
public Address CustomerAddress { get; set; }
}
多个对象的映射类:
public class CustomerDTO
{
public string Name { get; set; }
public string Mobile { get; set; }
public string State { get; set; }
public string City { get; set; }
public string Street { get; set; }
}
映射的代码:
public class Mapping
{
public static ProductDTO CreateProductMapping()
{
var map=Mapper.CreateMap<Product, ProductDTO>();
Product p = new Product
{
ProductName = "P1",
ProductUnitPrice = 100
}; var pdto = Mapper.Map<Product, ProductDTO>(p);
return pdto; } public static CustomerDTO CreateCustomerMapping()
{
//创建映射关系
var map = Mapper.CreateMap<Customer, CustomerDTO>();
map.ForMember(ppt => ppt.Name, p => p.MapFrom(s => s.CustomerName));
map.ForMember(ppt => ppt.Mobile, p => p.MapFrom(s => s.CustomerMobile));
map.ForMember(ppt => ppt.State, p => p.MapFrom(s => s.CustomerAddress.AddressState));
map.ForMember(ppt => ppt.City, p => p.MapFrom(s => s.CustomerAddress.AddressCity));
map.ForMember(ppt => ppt.Street, p => p.MapFrom(s => s.CustomerAddress.AddressStreet)); Customer customer = new Customer
{
CustomerName = "sun",
CustomerMobile = "135933",
CustomerAddress =
new Address
{
AddressState = "山西",
AddressCity = "孝义",
AddressStreet = "三贤路"
}
};
var customerdto = Mapper.Map<Customer, CustomerDTO>(customer);
return customerdto;
}
//简单
public static CustomerDTO CreateCustomerMapppingNew()
{
var map = Mapper.CreateMap<Customer, CustomerDTO>();
map.ConstructProjectionUsing(s => new CustomerDTO
{
Name = s.CustomerName,
Mobile = s.CustomerMobile,
State = s.CustomerAddress.AddressState,
City = s.CustomerAddress.AddressCity,
Street = s.CustomerAddress.AddressStreet
});
Customer customer = new Customer
{
CustomerName = "sun",
CustomerMobile = "13458629365",
CustomerAddress =
new Address
{
AddressState = "山西",
AddressCity = "孝义",
AddressStreet = "三贤路"
}
};
var customerdto = Mapper.Map<Customer, CustomerDTO>(customer);
return customerdto;
} }
在上下文接口中(IRepositoryContext)中定义DTO的支持:
//添加DTO对象
void RegisterCreateDTO<TDTO, TAggreateRoot>(TDTO tdto, Action<TDTO> processdto)
where TAggreateRoot :
class, IAggreateRoot; void RegisterUpdate<TAggreateRoot>(TAggreateRoot aggreateroot) where TAggreateRoot :
class, IAggreateRoot;
void RegisterUpdateDTO<TDTO, TAggreateRoot>(TDTO tdto, Action<TDTO> processdto) where TAggreateRoot :
class, IAggreateRoot;
仓储接口的调整:
void Create<TDTO>(TDTO tdto); TDTO GetByID<TDTO>(Guid id); List<TDTO> GetByCondition<TDTO>(Expression<Func<TAggreateRoot, bool>> condition); void Update<TDTO>(TDTO tdto); void Remove<TDTO>(TDTO tdto);
上下文DTO定义:(让EF上下文实现)
public abstract void RegisterCreateDTO<TDTO, TAggreateRoot>(TDTO tdto, Action<TDTO> processdto)
where TAggreateRoot : class, IAggreateRoot; public abstract void RegisterUpdateDTO<TDTO, TAggreateRoot>(TDTO tdto, Action<TDTO> processdto)
where TAggreateRoot : class, IAggreateRoot; public abstract void RegisterRemoveDTO<TDTO, TAggreateRoot>(TDTO tdto, Action<TDTO> processdto)
where TAggreateRoot : class, IAggreateRoot;
EF上下文中实现:
public override void RegisterCreateDTO<TDTO, TAggreateRoot>(TDTO tdto, Action<TDTO> processdto)
{ if (processdto != null)
processdto(tdto);
var aggreateroot = Mapper.Map<TDTO, TAggreateRoot>(tdto);
RegisterCreate(aggreateroot); } public override void RegisterUpdateDTO<TDTO, TAggreateRoot>(TDTO tdto, Action<TDTO> processdto)
{
if (processdto != null)
processdto(tdto);
var aggreateroot = Mapper.Map<TDTO, TAggreateRoot>(tdto);
RegisterUpdate(aggreateroot);
} public override void RegisterRemoveDTO<TDTO, TAggreateRoot>(TDTO tdto, Action<TDTO> processdto)
{
if (processdto != null)
processdto(tdto);
var aggreateroot = Mapper.Map<TDTO, TAggreateRoot>(tdto);
RegisterUpdate(aggreateroot);
}
EFRepository EF 仓储中的实现:
public void Create<TDTO>(TDTO tdto)
{
base.RegisterCreateDTO<TDTO, TAggreateRoot>(tdto, null);
} public List<TDTO> GetByCondition<TDTO>(Expression<Func<TAggreateRoot, bool>> condition)
{
//找到聚合根
var aggreatroots = orderdbcontext.Set<TAggreateRoot>().Where(condition).ToList();
//转黄成TDTO列表
var tdtos = new List<TDTO>();
if (aggreatroots.Count > 0)
{
foreach (var aggreateroot in aggreatroots)
{
var tdto = Mapper.Map<TAggreateRoot, TDTO>(aggreateroot);
tdtos.Add(tdto);
}
}
return tdtos;
} public TDTO GetByID<TDTO>(Guid id)
{
//得到聚合根
var aggreateroot = orderdbcontext.Set<TAggreateRoot>().Where(p => p.Id == id).SingleOrDefault();
//进行类型转换
var tdto = Mapper.Map<TAggreateRoot, TDTO>(aggreateroot);
//返回对象
return tdto;
} public void Remove<TDTO>(TDTO dto)
{
RegisterRemoveDTO<TDTO, TAggreateRoot>(dto, null);
} public void Update<TDTO>(TDTO dto)
{
RegisterUpdateDTO<TDTO, TAggreateRoot>(dto, null);
}
定义产品DTO类:
public class ProductDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public string Size { get; set; }
public int Amount { get; set; }
public decimal UnitPrice { get; set; }
public string PCategoryName { get; set; }
public string PDescription { get; set; }
}
在ProductCategory中实现构造函数:
public ProductCategory()
{ }
在Product中实现DTO:
public Product(IRepository<Product> irepository)
{
this.irepository = irepository;
//完成映射的创建
ProductMapping();
} //在调用构造函数的时候完成映射的创建
private void ProductMapping()
{
//从界面的DTO持久化领域对象
var mapin = Mapper.CreateMap<ProductDTO, Product>();
//指定属性的对应关系
mapin.ConstructProjectionUsing(p => new Product
{
ProductName = p.Name,
Size = p.Size,
Color = p.Color,
Count = p.Amount,
UnitPrice = p.UnitPrice,
ProductCategory = new ProductCategory
{
Id = Guid.NewGuid(),
CategoryName = p.PCategoryName,
Description = p.PDescription
}
});
//返回界面的东西
var mapout = Mapper.CreateMap<Product, ProductDTO>();
mapout.ConstructProjectionUsing(p => new ProductDTO
{
Name = p.ProductName,
Size = p.Size,
Color = p.Color,
UnitPrice = p.UnitPrice,
PCategoryName = p.ProductCategory.CategoryName,
PDescription = p.ProductCategory.Description,
Amount = p.Count
});
}
得到产品DTO 的方法:
/// <summary>
/// 得到产品DTO
/// </summary>
/// <param name="productname"></param>
/// <returns></returns>
public Product GetProducyByName(string productname)
{
return irepository.GetByCondition(p => p.ProductName == productname)
.FirstOrDefault();
}
通过DTO 创建Product:
/// <summary>
/// 通过DTOCreate
/// </summary>
/// <param name="productdto"></param>
public void CreateProduct(ProductDTO productdto)
{
productdto.Id = Guid.NewGuid();
irepository.Create(productdto);
}
服务 DTO 的创建:
/// <summary>
/// 服务DTO的创建
/// </summary>
/// <param name="productdto"></param>
public void Createproduct(ProductDTO productdto)
{
product.CreateProduct(productdto);
context.Commit();
}
/// <summary>
/// DTO的查询
/// </summary>
/// <param name="productname"></param>
/// <returns></returns>
public ProductDTO GetProductDTOByName(string productname)
{
return product.GetProductDTOByName(productname);
}
通过DTO创建测试:
[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", "运动类产品");
ProductDTO productdto = new ProductDTO();
productdto.Name = "P3";
productdto.Color = "Blue";
productdto.Size = "Middle";
productdto.Amount = 5000;
productdto.UnitPrice = 10;
productdto.PCategoryName = "C3";
productdto.PDescription = "正装";
product.Createproduct(productdto);
context.Commit();
Assert.IsNotNull(product.GetProductByName("P3")); } [TestMethod]
public void GetproductDTOByName()
{
ProductAppService productappservice =
new ProductAppService();
var productdto = productappservice.GetProductDTOByName("P3");
Assert.AreEqual("P3", productdto.Name);
Assert.AreEqual("C3", productdto.PCategoryName);
}
DDD领域模型AutoMapper实现DTO(七)的更多相关文章
- 使用AutoMapper实现Dto和Model之间自由转换
应用场景:一个Web应用通过前端收集用户的输入成为Dto,然后将Dto转换成领域模型并持久化到数据库中.另一方面,当用户请求数据时,我们又需要做相反的工作:将从数据库中查询出来的领域模型以相反的方式转 ...
- 自制AutoMapper实现DTO到持久层Entity的转换
自制AutoMapper实现DTO到持久层Entity的转换 项目中经常涉及到页面DTO更新,保存到数据库的操作,这就必然牵扯到DTO和持久层对象的转换,常见的第三方库有: java:dozer .n ...
- DDD领域模型和充血对象
DDD领域模型 官方说法 领域驱动设计,它是对面向对象的的分析和设计(OOAD,Object Orient Analysis Design)的一个补充,对技术框架进行了分层规划,同时对每个类进行了策略 ...
- 简单使用AutoMapper实现DTO转换
DTO(Data Transfer Object)数据传输对象,只是传输数据,完成与领域对象之间的转换,并不包含领域业务处理. 当领域模型设计人员只关注核心业务,满足于领域模型的精巧,而不关心具体实现 ...
- AutoMapper完成Dto与Model的转换
在实际的软件开发项目中,我们的“业务逻辑”常常需要我们对同样的数据进行各种变换. 例如,一个Web应用通过前端收集用户的输入成为Dto,然后将Dto转换成领域模型并持久化到数据库中.相反,当用户请求数 ...
- 使用AutoMapper实现Dto和Model的自由转换
AutoMapper是一个.NET的对象映射工具. 项目地址:https://github.com/AutoMapper/AutoMapper. 帮助文档:https://github.com/Aut ...
- 使用AutoMapper实现Dto和Model的自由转换(上)
在实际的软件开发项目中,我们的“业务逻辑”常常需要我们对同样的数据进行各种变换.例如,一个Web应用通过前端收集用户的输入成为Dto,然后将Dto转换成领域模型并持久化到数据库中.另一方面,当用户请求 ...
- DDD领域模型查询方法实现(八)
在DDD.Domain工程文件夹Repository下创建RequestPage类: public class RequestPage { public RequestPage(int pagesiz ...
- 使用AutoMapper实现Dto和Model的自由转换(下)
书接上文.在上一篇文章中我们讨论了使用AutoMapper实现类型间1-1映射的两种方式——Convention和Configuration,知道了如何进行简单的OO Mapping.在这个系列的最后 ...
随机推荐
- ThreadLocal以及内存泄漏
ThreadLocal是什么 ThreadLocal 的作用是提供线程内的局部变量,这种变量在线程的生命周期内起作用,减少同一个线程内多个函数或者组件之间一些公共变量的传递的复杂度.但是如果滥用Thr ...
- Every-SG游戏
参考自 石家庄二中 贾志豪 IOI2009国家集训队论文 <组合游戏略述—— 浅谈 SG 游戏的若干拓展及变形> 一.定义 游戏规则加上 对于还没有结束的所有单一游戏,游戏者必须对其进行决 ...
- 转--python 中写单例
原文地址 原文地址2 Python中的单例模式的几种实现方式的及优化 阅读目录(Content) 单例模式 实现单例模式的几种方式 1.使用模块 2.使用装饰器 3.使用类 4.基于__new__方法 ...
- JMS学习(八)-ActiveMQ Consumer 使用 push 还是 pull 获取消息
ActiveMQ是一个消息中间件,对于消费者而言有两种方式从消息中间件获取消息: ①Push方式:由消息中间件主动地将消息推送给消费者:②Pull方式:由消费者主动向消息中间件拉取消息.看一段官网对P ...
- 5W2H分析法
- C# 反编译项目修复
1.反编译测试程序 1>.将测试程序添加到.NET Reflector 2>.选中测试程序后右键选择导出 2.反编译项目修复 1>.问题一 问题现象: base.AutoScaleM ...
- linux笔记_day04
1.cat 连接并显示 -n 显示行号 -E END 显示行尾 2.tac 从后往前显示 3.ctrl +C 4.more 向后翻 到最后会退出 5.less 翻到最后不退出 常用 支持b k sp ...
- SpringMVC使用Hession发布远程服务
(1)三个项目,Api(存放提供者和消费者共有的xx,例如实体类以及服务接口等等).Service(服务提供者).Provider(服务消费者) Api部分代码 package cn.coreqi.e ...
- C# 无法识别的消息版本。
问题:最近跟OA的java项目做审核接口,调用接口时提示"无法识别的消息版本.“ 解决:一直以为是协议不兼容,查了半天,最终发现是这个项目.net framework版本太低,升级为高版本即 ...
- 使用密钥认证机制远程登录Linux
密钥认证机制 创建存放key的文件 1)创建目录 /root/.ssh 并设置权限 [root@localhost ~]# mkdir /root/.ssh mkdir 命令用来创建目录,以后会详细介 ...