1、Initialize:Mapper 初始化

Mapper.Initialize(x =>x.CreateMap<Destination, Source>()
);Mapper.Initialize(cfg =>
{cfg.CreateMap<Aliens, Person>();
});Mapper.Initialize(cfg => {cfg.CreateMap<Foo, FooDto>();cfg.CreateMap<Bar, BarDto>();
});

2、映射前、映射后 操作

Mapper.Initialize(x =>x.CreateMap<Destination, Source>().BeforeMap((src, dest) => src.InfoUrl = "https://" + src.InfoUrl).AfterMap((src, dest) => src.name = "真棒" + src.name));cfg.CreateMap<ClassBannerDo, ClassBannerDto>().BeforeMap((source, dto) =>{if (!string.IsNullOrEmpty(source.ImgUrl))source.ImgUrl = StringPlus.GetWebConfigKey("ImageDomain") + source.ImgUrl;if (!string.IsNullOrEmpty(source.Thumbnail))source.Thumbnail = StringPlus.GetWebConfigKey("ImageDomain") + source.Thumbnail;});cfg.CreateMap<StudentInoutDo, StudentInoutDto>()// f.字符串 f => 枚举描述.ForMember(f => f.InOutDisplay, f => f.MapFrom(m => m.Inout == 1 ? EnumInout.In.GetEnumDescription() : EnumInout.Out.GetEnumDescription()));// f.枚举 f => 枚举.ForMember(f => f.InOutEnum, f => f.MapFrom(m => m.Inout == 1 ? EnumInout.In : EnumInout.Out))// 实体对象序列化 json 字符串.ForMember(f => f.School, f => f.MapFrom(m => JsonConvert.SerializeObject(m.School))).ReverseMap()// 数据库中为 json 字符串,反序列化 实体对象.ForMember(f => f.School, f => f.MapFrom(m => JsonConvert.DeserializeObject<SchoolDo>(m.School)));public enum EnumInout
{Normal = 0,[Description("进校")]In = 1,[Description("出校")]Out = 2
}

3、条件映射

Mapper.Initialize(x => x.CreateMap<Destination, Source>().ForMember(dest => dest.InfoUrl, opt => opt.Condition(dest => dest.InfoUrl == "www.cnblogs.com/zaranet1")).ForMember(...(.ForMember(...))));

4、多类映射

public static void Configure()
{Mapper.Initialize(cfg =>{cfg.AddProfile<DestinationSourceProfile>();cfg.AddProfile(new StudentSourceProfile());});
}

5、CreateMap【配置 AutoMapper】 与 Mapper.Map【执行映射,执行 mapping】

Mapper.CreateMap<Order, OrderDto>();
OrderDto dto = Mapper.Map<Order, OrderDto>(order);
var fooDto = Mapper.Map<FooDto>(foo);
var barDto = Mapper.Map<BarDto>(bar);

6、忽略字段,隐藏字段

Mapper.CreateMap<Source, Destination>().ForMember(dest => dest.SomeValuefff, opt => opt.Ignore());

==============.NET Core AutoMapper==============

1、Nuget安装

AutoMapper
AutoMapper.Extensions.Microsoft.DependencyInjection  //需要依赖注入AutoMapper,需要下载该包。

2、在Startup中添加AutoMapper

public void ConfigureServices(IServiceCollection services)
{services.AddMvc();//添加对AutoMapper的支持services.AddAutoMapper(typeof(WebMapperInit));
}

3、创建AutoMapper映射配置文件

using AutoMapper;
using Hospital.Entity;
using Hospital.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;namespace Hospital.WebAPI
{public class WebMapperInit : Profile{public WebMapperInit(){CreateMap<base_patient, PatientRequest>().ForMember(d => d.Id, option => option.MapFrom(s => s.id)).ForMember(d => d.UserId, option => option.MapFrom(s => s.user_id)).ForMember(d => d.HospitalId, option => option.MapFrom(s => s.hospital_id)).ForMember(d => d.CreatedTime, option => option.MapFrom(s => s.created_time)).ForMember(d => d.ModifyTime, option => option.MapFrom(s => s.modify_time)).ForMember(d => d.Status, option => option.MapFrom(s => s.status)).ReverseMap();}}
}

4、Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Hospital.Entity;
using Hospital.Models;
using Hospital.Repository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;namespace Hospital.WebAPI.Controllers
{[Route("")][ApiController]public class PatientController : ControllerBase{private readonly IPatientRepository _patientRepo;private readonly IMapper _mapper;public PatientController(IMapper mapper, IPatientRepository patientRepo){_patientRepo = patientRepo;_mapper = mapper;}[HttpPost]public CreatePatientResponse C([FromBody]PatientRequest request){request.HospitalId = Guid.NewGuid();_patientRepo.AddWithGuid(_mapper.Map<base_patient>(request));return new CreatePatientResponse();}}
}

5、静态文件

--
var config = new MapperConfiguration(cfg => cfg.CreateMap<E, T>());
var mapper = config.CreateMapper();
mapper.Map<T>--
public UserRepository(AssetDbContext context) : base(context)
{var config = new MapperConfiguration(cfg => cfg.CreateMap<UserDto, UserDo>());var mapper = config.CreateMapper();
}

6、
*、

AutoMapper 基本使用相关推荐

  1. [AutoMapper]反射自动注册AutoMapper Profile

    AutoMapper 帮我我们方便管理物件跟物件之间属性值格式转换 模型转换 这里有两个类别 UserInfoModel 当作我们从DB捞取出来模型资料 public class UserInfoMo ...

  2. AutoMapper用法

    AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 作者:齐飞 原文:http://www.qeefee.com/article/auto ...

  3. java automapper 使用_19.AutoMapper 之开放式泛型(Open Generics)

    开放式泛型(Open Generics) AutoMapper可以支持开放式泛型的映射.为开放式泛型创建映射: public class Source { public T Value { get; ...

  4. C# AutoMapper的简单扩展

    AutoMapper可以很方便的将一个实体的属性值转化给另一个对象.这个功能在我们日常的编码中经常会遇到.我将AutoMapper的一些基本映射功能做成扩展方法,在编码中更方便使用. using Sy ...

  5. 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)

    前言 上一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](二)>我们通过如下操作: 创建实体及工具类 创建Re ...

  6. automapper java 有什么_对象映射工具AutoMapper介绍

    AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间. 一. AutoMapper ...

  7. automapper

    1.https://www.cnblogs.com/youring2/p/automapper.html 这个讲知识点, 理解automapper 的 概念. 解释的不错,但是有些技术已经过时了,即已 ...

  8. AutoMapper 入门

    简述 开篇,介绍一下AutoMapper,欢迎. AutoMapper是什么:通过预先配置,将两个不同类型的对象进行转换的工具. AutoMapper干什么用:更方便的转换两个不同类型的对象. Aut ...

  9. C#进阶系列——DDD领域驱动设计初探(五):AutoMapper使用

    前言:前篇搭建了下WCF的代码,就提到了DTO的概念,对于为什么要有这么一个DTO的对象,上章可能对于这点不太详尽,在此不厌其烦再来提提它的作用: 从安全上面考虑,领域Model都带有领域业务,让Cl ...

  10. AutoMapper入门使用

    AutoMapper入门使用 在应用开发的过程中,首先要了解整个系统中各个系统的组件的作用,然后了解系统的工作流(workflow),最后需要梳理一遍数据流(dataflow),而在整理数据流的过程中 ...

最新文章

  1. IOS7原生API进行二维码条形码的扫描
  2. Android程序的反编译对抗研究
  3. 30亿美金投入!一文读懂英伟达性能凶残的Tesla V100牛在哪?
  4. 每天一道LeetCode-----将字符串拆分成有效的ip地址
  5. 判断一个数组中的值是否在另一个数组中
  6. eclipse导入myeclipse项目
  7. (转)shiro权限框架详解01-权限理论介绍
  8. 怎么将py文件转成dll_怎样将PDF文件转成CAD图纸?
  9. opencv 实现角点检测 Shi-Tomasi角点检测
  10. 从零学React Native之01创建第一个程序
  11. 乒乓球单循环赛_乒乓球单循环比赛如何计算得分排名(实用方法)
  12. tp5 验证码 验证不正确 (跨域问题)
  13. 中国天气网 城市代号
  14. YOLOv5中的CSP结构
  15. android 国际化
  16. 计算机网络换算方法,网络带宽换算
  17. 编译QT项目出现错误:error C2144: syntax error : 'void' should be preceded by ';'
  18. Python项目实战:各种小说姓名生成器
  19. 计算机毕业设计JavaBS高校教师考勤系统(源码+系统+mysql数据库+lw文档)
  20. house of apple2(改进)

热门文章

  1. 2017年1月英语总结
  2. mysql长轮询_【系列一】ajax长轮询、轮询应用和介绍
  3. 你的成长启蒙伙伴——动画
  4. matplot模块中的pylab
  5. Illustrator 教程:如何在 Illustrator 中描摹对象?
  6. MySql忘记root密码处理方式
  7. 支付宝9亿红包分钱了,你拿了多少钱?有人领到18888元吗?
  8. 持久续航+反向充电,华为畅享MAX治好了低电量恐惧症
  9. 原生js字符串拼接问题(拼接字符串和变量,input框的value绑定变量)
  10. “苹果正在走下神坛”