AutoMapper 基本使用
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 基本使用相关推荐
- [AutoMapper]反射自动注册AutoMapper Profile
AutoMapper 帮我我们方便管理物件跟物件之间属性值格式转换 模型转换 这里有两个类别 UserInfoModel 当作我们从DB捞取出来模型资料 public class UserInfoMo ...
- AutoMapper用法
AutoMapper是对象到对象的映射工具.在完成映射规则之后,AutoMapper可以将源对象转换为目标对象. 作者:齐飞 原文:http://www.qeefee.com/article/auto ...
- java automapper 使用_19.AutoMapper 之开放式泛型(Open Generics)
开放式泛型(Open Generics) AutoMapper可以支持开放式泛型的映射.为开放式泛型创建映射: public class Source { public T Value { get; ...
- C# AutoMapper的简单扩展
AutoMapper可以很方便的将一个实体的属性值转化给另一个对象.这个功能在我们日常的编码中经常会遇到.我将AutoMapper的一些基本映射功能做成扩展方法,在编码中更方便使用. using Sy ...
- 一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](三)
前言 上一篇<一步一步创建ASP.NET MVC5程序[Repository+Autofac+Automapper+SqlSugar](二)>我们通过如下操作: 创建实体及工具类 创建Re ...
- automapper java 有什么_对象映射工具AutoMapper介绍
AutoMapper是用来解决对象之间映射转换的类库.对于我们开发人员来说,写对象之间互相转换的代码是一件极其浪费生命的事情,AutoMapper能够帮助我们节省不少时间. 一. AutoMapper ...
- automapper
1.https://www.cnblogs.com/youring2/p/automapper.html 这个讲知识点, 理解automapper 的 概念. 解释的不错,但是有些技术已经过时了,即已 ...
- AutoMapper 入门
简述 开篇,介绍一下AutoMapper,欢迎. AutoMapper是什么:通过预先配置,将两个不同类型的对象进行转换的工具. AutoMapper干什么用:更方便的转换两个不同类型的对象. Aut ...
- C#进阶系列——DDD领域驱动设计初探(五):AutoMapper使用
前言:前篇搭建了下WCF的代码,就提到了DTO的概念,对于为什么要有这么一个DTO的对象,上章可能对于这点不太详尽,在此不厌其烦再来提提它的作用: 从安全上面考虑,领域Model都带有领域业务,让Cl ...
- AutoMapper入门使用
AutoMapper入门使用 在应用开发的过程中,首先要了解整个系统中各个系统的组件的作用,然后了解系统的工作流(workflow),最后需要梳理一遍数据流(dataflow),而在整理数据流的过程中 ...
最新文章
- IOS7原生API进行二维码条形码的扫描
- Android程序的反编译对抗研究
- 30亿美金投入!一文读懂英伟达性能凶残的Tesla V100牛在哪?
- 每天一道LeetCode-----将字符串拆分成有效的ip地址
- 判断一个数组中的值是否在另一个数组中
- eclipse导入myeclipse项目
- (转)shiro权限框架详解01-权限理论介绍
- 怎么将py文件转成dll_怎样将PDF文件转成CAD图纸?
- opencv 实现角点检测 Shi-Tomasi角点检测
- 从零学React Native之01创建第一个程序
- 乒乓球单循环赛_乒乓球单循环比赛如何计算得分排名(实用方法)
- tp5 验证码 验证不正确 (跨域问题)
- 中国天气网 城市代号
- YOLOv5中的CSP结构
- android 国际化
- 计算机网络换算方法,网络带宽换算
- 编译QT项目出现错误:error C2144: syntax error : 'void' should be preceded by ';'
- Python项目实战:各种小说姓名生成器
- 计算机毕业设计JavaBS高校教师考勤系统(源码+系统+mysql数据库+lw文档)
- house of apple2(改进)