说说AutoMapper那些事
项目中用到了DTO与Model之间的转换,因为model项目比较多,所以需要使用工具或者代码来实现快速的转换。AutoMapper就是一个很好的基于约定的object-object mapper.映射器。
Map规则:
AutoMapper默认是根据实体的属性名称来一一对应映射,你也可以手动的设置Map规则。
接下来举个栗子:
一、默认属性Map (DTO => Model)
准备实体
namespace MapDemo
{
using System;
using System.Collections.Generic; public partial class Service
{
public Service()
{
this.ServiceDtl = new HashSet<ServiceDtl>();
} public int Id { get; set; }
public string Name { get; set; }
public Nullable<decimal> Price { get; set; } public virtual ICollection<ServiceDtl> ServiceDtl { get; set; } }
}
namespace MapDemo
{
using System;
using System.Collections.Generic; public partial class ServiceDtl
{
public int DtlId { get; set; }
public string DtlName { get; set; }
public Nullable<int> Id { get; set; } public virtual Service Service { get; set; }
}
}
准备Model
namespace MapDemo.Model
{
class ServiceModel
{
public int Id { get; set; }
public string Name { get; set; }
public Nullable<decimal> Price { get; set; } public virtual ICollection<ServiceDtl> ServiceDtl { get; set; } }
}
namespace MapDemo.Model
{
class ServiceDtlModel
{
public int DtlId { get; set; }
public string DtlName { get; set; }
public Nullable<int> Id { get; set; } public virtual Service Service { get; set; }
}
}
Mapper初始化:
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Service, ServiceModel>();
});
二、单表Map
var serviceModel= Mapper.Map<ServiceModel>(service);
三、多表多层指定Map
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Service, ServiceModel>().ForMember(d => d.ServiceDtl, opt => opt.MapFrom(s => s.ServiceDtl));
cfg.CreateMap<ServiceDtl, ServiceDtlModel>();
}); var serviceModel= Mapper.Map<ServiceModel>(service);
这样service中就包含了ServiceDtl的项目,操作DB的时候进行反向Map,只需要操作主表,适用于多层表结构的操作,方便快捷,代码量少。
四、Model与Model之间Map
创建一个类似ServiceModel的模型CYService
namespace MapDemo.Model
{
class CYService
{ public int Id { get; set; }
public string Name { get; set; }
public Nullable<decimal> Price { get; set; } }
}
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Service, CYService>();
});
var cyService = Mapper.Map<CYService>(newItem);
简单总结一下:
AutoMapper对于多表的层级操作十分方便,并且易于扩展。
但是项目中不建议直接使用Mapper.Map(),建议使用局部的mapEngine.Map()避免全局Map的影响。
具体参照官方解释:http://automapper.readthedocs.io/en/latest/index.html
说说AutoMapper那些事的更多相关文章
- 【道德经】漫谈实体、对象、DTO及AutoMapper的使用
写在前面 实体和值对象 实体和对象 故常无欲以观其妙,常有欲以观其徼 初始实体和演化实体 代码中的DTO AutoMapper实体转换 后记 实体(Entity).对象(Object).DTO(Dat ...
- 结婚虽易,终老不易:EntityFramework和AutoMapper的婚后生活
写在前面 我到底是什么? 越界的可怕 做好自己 后记 上一篇<恋爱虽易,相处不易:当EntityFramework爱上AutoMapper>文章的最后提到,虽然AutoMapper为了En ...
- C#进阶系列——DDD领域驱动设计初探(五):AutoMapper使用
前言:前篇搭建了下WCF的代码,就提到了DTO的概念,对于为什么要有这么一个DTO的对象,上章可能对于这点不太详尽,在此不厌其烦再来提提它的作用: 从安全上面考虑,领域Model都带有领域业务,让Cl ...
- AutoMapper Getting started
AutoMapper 是什么? 为什么要用AutoMapper? 如何使用AutoMapper? 在什么地方配置AutoMapper? 如何测试my mappings? AutoMapper 是什么? ...
- DTO学习系列之AutoMapper(六)----EntityFramework和AutoMapper的婚后生活
写在前面 我到底是什么? 越界的可怕 做好自己 后记 文章标题主要关键字:mapping DTOs to Entities,注意并不是“Entities to DTOs”,表示实体对象到DTO的转换, ...
- DTO学习系列之AutoMapper(一)
一.前言 DTO(Data Transfer Object)数据传输对象,注意关键字“数据”两个字,并不是对象传输对象(Object Transfer Object),所以只是传输数据,并不包含领域业 ...
- .NET的DTO映射工具AutoMapper
.NET的DTO映射工具AutoMapper 原文:https://github.com/AutoMapper/AutoMapper/wiki/Getting-started 参考:http://ww ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](五)
前言 Hi,大家好,我是Rector 时间飞逝,一个星期又过去了,今天还是星期五,Rector在图享网继续跟大家分享系列文本:一步一步创建ASP.NET MVC5程序[Repository+Autof ...
- 使用.Net Core Mvc +SqlSugar +Autofac+AutoMapper+....
开源地址:https://github.com/AjuPrince/Aju.Carefree 目前用到了 SqlSugar.Dapper.Autofac.AutoMapper.Swagger.Redi ...
随机推荐
- 易百教程人工智能python修正-人工智能数据准备-预处理数据
预处理数据 在我们的日常生活中,需要处理大量数据,但这些数据是原始数据. 为了提供数据作为机器学习算法的输入,需要将其转换为有意义的数据. 这就是数据预处理进入图像的地方. 换言之,可以说在将数据提供 ...
- flutter isolate demo
main.dart import 'package:flutter/material.dart'; import 'package:flutter_isolate/flutter_isolate.da ...
- python day4 元组/字典/集合类知识点补充
目录 python day4 元组/字典/集合类知识点补充 1. 元组tuple知识点补充 2. 字典dict的知识点补充 3. 基本数据类型set 4. 三元运算,又叫三目运算 5. 深复制浅复制 ...
- 爬虫之PyQuery的base了解
爬虫之PyQuery的base了解 pyquery库是jQuery的Python实现,能够以jQuery的语法来操作解析 HTML 文档,易用性和解析速度都很好,和它差不多的还有BeautifulSo ...
- Vivado debug异常现象
前言 bit文件和ltx文件的信号位宽不匹配问题.用了dont_touch等属性没用... WARNING: [Labtools 27-1972] Mismatch between the desig ...
- iOS应用图片尺寸制作脚本
1.前提说明 通常 2.代码使用说明 2.1 脚本基本代码 #!/bin/sh iPhoneIcon() { sips -z 30 25 if_connected_green.png --out ./ ...
- 代替for-in 遍历对象
object.keys() object.getOwnPropertyName()
- CentOS7编译安装httpd-2.4.41 php7.3
CentOS7编译安装httpd-2.4.41 php7.3 安装参考环境: CentOS Linux release 7.5.1804 (Core) 一.安装依赖包 httpd安装的依赖包 # yu ...
- nginx 代理 websocket
nginx 代理 websocket nginx 首先确认版本必须是1.3以上 map指令的作用: 该作用主要是根据客户端请求中$http_upgrade 的值,来构造改变$connection_up ...
- 【独家】K8S漏洞报告 | 近期bug fix解读
安全漏洞CVE-2019-3874分析 Kubernetes近期重要bug fix分析 Kubernetes v1.13.5 bug fix数据分析 ——本周更新内容 安全漏洞CVE-2019-387 ...