2.x 中使用
简单封装下,支持直接通过autoInject(sourceType,tagettype,tagettype2)注解方式直接映射,也支持创建profile方式 两种使用方式参考: [Route("TestParamValide")] [HttpPost] public ActionResult TestParamValide([FromBody]ValideModel model) { var sysDic = model.MapTo(); return null; }
两种使用方式:
// -----------------------------------------------------------------------
// <copyright file="MqSettings.cs" company="禾卓软件科技有限公司">
// Copyright (c) 2020 禾卓软件科技有限公司. All rights reserved.
// </copyright>
// <last-editor>谭明超 - 2020/4/28 16:29:38 </last-editor>
// -----------------------------------------------------------------------
[Route("TestParamValideMapperProfile")] [HttpPost] public ActionResult<object> TestParamValideMapperProfile([FromBody]ValideModel2 model) { var sysDic = model.MapTo<SysDictionary>(); return null; } //方式一:直接使用AutoInject [AutoInject(typeof(SysDictionary), typeof(ValideModel))] public class ValideModel : Infrastructure.Mapper.DTO { [Required(ErrorMessage = "{0}不可为空")] public string Code { get; set; } } public class ValideModel2 : Infrastructure.Mapper.DTO { [Required(ErrorMessage = "{0}不可为空")] public string Code { get; set; } } //方式二:创建Profile public class ValideModelMapperProfile : Profile, IMapperProfile { public ValideModelMapperProfile() { CreateMap<SysDictionary, ValideModel2>().ReverseMap(); } }
源码地址:
https://github.com/tanmingchao/automapper/tree/master
原文:https://www.cnblogs.com/Tmc-Blog/p/12795633.html