一、最简单的用法

有两个类User和UserDto

  1. 1 public class User
  2. 2 {
  3. 3 public int Id { get; set; }
  4. 4 public string Name { get; set; }
  5. 5 public int Age { get; set; }
  6. 6 }
  7. 7
  8. 8 public class UserDto
  9. 9 {
  10. 10 public string Name { get; set; }
  11. 11 public int Age { get; set; }
  12. 12 }

将User转换成UserDto也和简单

  1. 1 Mapper.Initialize(x => x.CreateMap<User, UserDto>());
  2. 2 User user = new User()
  3. 3 {
  4. 4 Id = 1,
  5. 5 Name = "caoyc",
  6. 6 Age = 20
  7. 7 };
  8. 8 var dto = Mapper.Map<UserDto>(user);

这是一种最简单的使用,AutoMapper会更加字段名称去自动对于,忽略大小写。

二、如果属性名称不同

将UserDto的Name属性改成Name2

  1. 1 Mapper.Initialize(x =>
  2. 2 x.CreateMap<User, UserDto>()
  3. 3 .ForMember(d =>d.Name2, opt => {
  4. 4 opt.MapFrom(s => s.Name);
  5. 5 })
  6. 6 );
  7. 7
  8. 8 User user = new User()
  9. 9 {
  10. 10 Id = 1,
  11. 11 Name = "caoyc",
  12. 12 Age = 20
  13. 13 };
  14. 14
  15. 15 var dto = Mapper.Map<UserDto>(user);

三、使用Profile配置

自定义一个UserProfile类继承Profile,并重写Configure方法

  1. 1 public class UserProfile : Profile
  2. 2 {
  3. 3 protected override void Configure()
  4. 4 {
  5. 5 CreateMap<User, UserDto>()
  6. 6 .ForMember(d => d.Name2, opt =>
  7. 7 {
  8. 8 opt.MapFrom(s => s.Name);
  9. 9 });
  10. 10 }
  11. 11 }

使用时就这样

  1. 1 Mapper.Initialize(x => x.AddProfile<UserProfile>());
  2. 2
  3. 3 User user = new User()
  4. 4 {
  5. 5 Id = 1,
  6. 6 Name = "caoyc",
  7. 7 Age = 20
  8. 8 };
  9. 9
  10. 10 var dto = Mapper.Map<UserDto>(user);

四、空值替换NullSubstitute

空值替换允许我们将Source对象中的空值在转换为Destination的值的时候,使用指定的值来替换空值。

  1. 1 public class UserProfile : Profile
  2. 2 {
  3. 3 protected override void Configure()
  4. 4 {
  5. 5 CreateMap<User, UserDto>()
  6. 6 .ForMember(d => d.Name2, opt => opt.MapFrom(s => s.Name))
  7. 7 .ForMember(d => d.Name2, opt => opt.NullSubstitute("值为空"));
  8. 8
  9. 9 }
  10. 10 }
  1. 1 Mapper.Initialize(x => x.AddProfile<UserProfile>());
  2. 2
  3. 3 User user = new User()
  4. 4 {
  5. 5 Id = 1,
  6. 6 Age = 20
  7. 7 };
  8. 8
  9. 9 var dto = Mapper.Map<UserDto>(user);

结果为:

五、忽略属性Ignore

  1. 1 public class User
  2. 2 {
  3. 3 public int Id { get; set; }
  4. 4 public string Name { get; set; }
  5. 5 public int Age { get; set; }
  6. 6 }
  7. 7
  8. 8 public class UserDto
  9. 9 {
  10. 10 public string Name { get; set; }
  11. 11 public int Age { get; set; }
  12. 12
  13. 13 }
  14. 14
  15. 15 public class UserProfile : Profile
  16. 16 {
  17. 17 protected override void Configure()
  18. 18 {
  19. 19 CreateMap<User, UserDto>().ForMember("Name", opt => opt.Ignore());
  20. 20 }
  21. 21 }

使用

  1. 1 Mapper.Initialize(x => x.AddProfile<UserProfile>());
  2. 2
  3. 3 User user = new User()
  4. 4 {
  5. 5 Id = 1,
  6. 6 Name="caoyc",
  7. 7 Age = 20
  8. 8 };
  9. 9
  10. 10 var dto = Mapper.Map<UserDto>(user);

结果:

六、预设值

如果目标属性多于源属性,可以进行预设值

  1. 1 public class User
  2. 2 {
  3. 3 public int Id { get; set; }
  4. 4 public string Name { get; set; }
  5. 5 public int Age { get; set; }
  6. 6 }
  7. 7
  8. 8 public class UserDto
  9. 9 {
  10. 10 public string Name { get; set; }
  11. 11 public int Age { get; set; }
  12. 12 public string Gender { get; set; }
  13. 13
  14. 14 }
  15. 15
  16. 16 public class UserProfile : Profile
  17. 17 {
  18. 18 protected override void Configure()
  19. 19 {
  20. 20 CreateMap<User, UserDto>();
  21. 21 }
  22. 22 }

使用

  1. 1 Mapper.Initialize(x => x.AddProfile<UserProfile>());
  2. 2
  3. 3 User user = new User()
  4. 4 {
  5. 5 Id = 1,
  6. 6 Name="caoyc",
  7. 7 Age = 20
  8. 8 };
  9. 9
  10. 10 UserDto dto = new UserDto() {Gender = "男"};
  11. 11 Mapper.Map(user, dto);

七、类型转换ITypeConverter

如果数据中Gender存储的int类型,而DTO中Gender是String类型

  1. 1 public class User
  2. 2 {
  3. 3 public int Gender { get; set; }
  4. 4 }
  5. 5
  6. 6 public class UserDto
  7. 7 {
  8. 8 public string Gender { get; set; }
  9. 9 }

类型转换类,需要实现接口ITypeConverter

  1. 1 public class GenderTypeConvertert : ITypeConverter<int, string>
  2. 2 {
  3. 3 public string Convert(int source, string destination, ResolutionContext context)
  4. 4 {
  5. 5 switch (source)
  6. 6 {
  7. 7 case 0:
  8. 8 destination = "男";
  9. 9 break;
  10. 10 case 1:
  11. 11 destination = "女";
  12. 12 break;
  13. 13 default:
  14. 14 destination = "未知";
  15. 15 break;
  16. 16 }
  17. 17 return destination;
  18. 18 }
  19. 19 }

配置规则

  1. 1 public class UserProfile : Profile
  2. 2 {
  3. 3 protected override void Configure()
  4. 4 {
  5. 5 CreateMap<User, UserDto>();
  6. 6
  7. 7 CreateMap<int, string>().ConvertUsing<GenderTypeConvertert>();
  8. 8 //也可以写这样
  9. 9 //CreateMap<int, string>().ConvertUsing(new GenderTypeConvertert());
  10. 10 }
  11. 11 }

使用

  1. 1 Mapper.Initialize(x => x.AddProfile<UserProfile>());
  2. 2
  3. 3 User user0 = new User() { Gender = 0 };
  4. 4 User user1 = new User() { Gender = 1 };
  5. 5 User user2 = new User() { Gender = 2 };
  6. 6 var dto0= Mapper.Map<UserDto>(user0);
  7. 7 var dto1 = Mapper.Map<UserDto>(user1);
  8. 8 var dto2 = Mapper.Map<UserDto>(user2);
  9. 9
  10. 10 Console.WriteLine("dto0:{0}", dto0.Gender);
  11. 11 Console.WriteLine("dto1:{0}", dto1.Gender);
  12. 12 Console.WriteLine("dto2:{0}", dto2.Gender);

结果

八、条件约束Condition

当满足条件时才进行映射字段,例如人类年龄,假设我们现在人类年龄范围为0-200岁(这只是假设),只有满足在这个条件才进行映射

DTO和Entity

  1. 1 public class User
  2. 2 {
  3. 3 public int Age { get; set; }
  4. 4 }
  5. 5
  6. 6 public class UserDto
  7. 7 {
  8. 8 public int Age { get; set; }
  9. 9 }

Profile

  1. 1 public class UserProfile : Profile
  2. 2 {
  3. 3 protected override void Configure()
  4. 4 {
  5. 5 CreateMap<User, UserDto>().ForMember(dest=>dest.Age,opt=>opt.Condition(src=>src.Age>=0 && src.Age<=200));
  6. 6 }
  7. 7 }

使用代码

  1. 1 Mapper.Initialize(x => x.AddProfile<UserProfile>());
  2. 2
  3. 3 User user0 = new User() { Age = 1 };
  4. 4 User user1 = new User() { Age = 150 };
  5. 5 User user2 = new User() { Age = 201 };
  6. 6 var dto0= Mapper.Map<UserDto>(user0);
  7. 7 var dto1 = Mapper.Map<UserDto>(user1);
  8. 8 var dto2 = Mapper.Map<UserDto>(user2);
  9. 9
  10. 10 Console.WriteLine("dto0:{0}", dto0.Age);
  11. 11 Console.WriteLine("dto1:{0}", dto1.Age);
  12. 12 Console.WriteLine("dto2:{0}", dto2.Age);

输出结果

转载 c# automapper 使用(一) https://www.cnblogs.com/caoyc/p/6367828.html的更多相关文章

  1. 转载 mvc:message-converters简单介绍 https://www.cnblogs.com/liaojie970/p/7736098.html

    mvc:message-converters简单介绍 说说@ResponseBody注解,很明显这个注解就是将方法的返回值作为reponse的body部分.我们进一步分析下这个过程涉及到的内容,首先就 ...

  2. Spring MVC 向页面传值-Map、Model和ModelMap https://www.cnblogs.com/caoyc/p/5635878.html

    Spring MVC 向页面传值-Map.Model和ModelMap 除了使用ModelAndView方式外.还可以使用Map.Model和ModelMap来向前台页面创造 使用后面3种方式,都是在 ...

  3. 【redis】redis五大类 用法 【转载:https://www.cnblogs.com/yanan7890/p/6617305.html】

    转载地址:https://www.cnblogs.com/yanan7890/p/6617305.html

  4. Bootstrap-table 使用总结 转载https://www.cnblogs.com/laowangc/p/8875526.html

    一.什么是Bootstrap-table? 在业务系统开发中,对表格记录的查询.分页.排序等处理是非常常见的,在Web开发中,可以采用很多功能强大的插件来满足要求,且能极大的提高开发效率,本随笔介绍这 ...

  5. 使用java实现单链表(转载自:https://www.cnblogs.com/zhongyimeng/p/9945332.html)

    使用java实现单链表----(java中的引用就是指针)转载自:https://www.cnblogs.com/zhongyimeng/p/9945332.html ? 1 2 3 4 5 6 7 ...

  6. Autofac框架详解 转载https://www.cnblogs.com/lenmom/p/9081658.html

    一.组件 创建出来的对象需要从组件中来获取,组件的创建有如下4种(延续第一篇的Demo,仅仅变动所贴出的代码)方式: 1.类型创建RegisterType AutoFac能够通过反射检查一个类型,选择 ...

  7. 关于pipeline的一篇转载博文https://www.cnblogs.com/midhillzhou/p/5588958.html

    引用自https://www.cnblogs.com/midhillzhou/p/5588958.html 1.pipeline的产生 从一个现象说起,有一家咖啡吧生意特别好,每天来的客人络绎不绝,客 ...

  8. 转载 https://www.cnblogs.com/bobo-pcb/p/11708459.html

    https://www.cnblogs.com/bobo-pcb/p/11708459.html #### 1 用VMware 15.0+win10企业版 1次安装成功 20200124 2 不要用v ...

  9. 干货,不小心执行了rm -f,除了跑路,如何恢复?https://www.cnblogs.com/justmine/p/10359186.html

    前言 每当我们在生产环境服务器上执行rm命令时,总是提心吊胆的,因为一不小心执行了误删,然后就要准备跑路了,毕竟人不是机器,更何况机器也有bug,呵呵. 那么如果真的删除了不该删除的文件,比如数据库. ...

随机推荐

  1. Extjs4.2 rest 与webapi数据交互----顺便请教了程序员的路该怎么走

    这一章接着上一篇 对于Ext.data.Store 介紹 与总结,以及对以前代码的重构与优化 1.对于更新OnUpdate()函数的修改:先上代码: function OnUpdate(record) ...

  2. js jq输入框中按回车触发提交事件,用户在页面输入后按回车(Enter键)进行

    js jq输入框中按回车触发提交事件,用户在页面输入后按回车(Enter键)进行 代码如下: <!DOCTYPE html> <html lang="en" xm ...

  3. Java,第16天,属性与方法;

    public class 类名{ private double 财产 = 0://设一个财产的属性: public void 一个月工资(){ this.财产 +=4500: }//设一个方法增加财产 ...

  4. border-sizing属性详解和应用

    box-sizing用于更改用于计算元素宽度和高度的默认的 CSS 盒子模型.它有content-box.border-box和inherit三种取值.inherit指的是从父元素继承box-sizi ...

  5. MEF 插件式开发之 DotNetCore 中强大的 DI

    背景叙述 在前面几篇 MEF 插件式开发 系列博客中,我分别在 DotNet Framework 和 DotNet Core 两种框架下实验了 MEF 的简单实验,由于 DotNet Framewor ...

  6. Centos 7.x 安装 Docker-ce

    Centos 下安装 Docker-ce CentOS 7.0, CentOS 7.2: cat > /etc/yum.repos.d/docker-main.repo << -'E ...

  7. Django&Flask区别

    Flask Flask 本身只有一个内核,几乎所有的功能都需要用第三方的扩展来实现. Flask 没有默认使用的数据库,默认依赖两个外部库:Jinja2 模板引擎和 WSGI 工具箱(采用的时 Wer ...

  8. 本地存储之sessionStorage

    源码可以到GitHub上下载! sessionStorage: 关闭浏览器再打开将不保存数据   复制标签页会连同sessionStorage数据一同复制 复制链接地址打开网页不会复制seession ...

  9. webpack打包时排除其中一个css、js文件,或单独打包一个css、js文件

    在项目中经常会需要将一些接口的配合文件或者某些样式文件,分离出来单独打包,便于后期改动,这里我以css文件为例,介绍实现两种方法: 项目目录: 如上图所示,现在我需要将项目中的scBtn.css文件单 ...

  10. layer插件layer.photos()动态插入的图片无法正常显示

    layer插件layer.photos()动态插入的图片无法正常显示,点击后面插入的图片,显示的是之前的图片列表,再次点击又是正常 有朋友遇到同样的问题 http://fly.layui.com/ji ...