标签 标签 标签
- 一句话的事儿:
安装方式:使用vs自带的nuget管理工具,搜索AutoMapper ,选择第一个安装到你的项目即可。
我从网上找了一些资料,
参考网址:http://blog.csdn.net/csethcrm/article/details/52934325
下载了个demo,然后自己又写了一遍,我把AutoMapper 的使用分为两种:
1、viewmodel与实体的字段名字是一致的,viewmodel的字段可以与实体中的字段数量不一致。
还有一种情况是:源实体中的字段名字是Getxxx,那么viewmodel中对应的字段可以是xxx,也会自动对应赋值,比如我写的demo中源实体中GetA,viewmodel中的A;
再有一种情况就是实体中的实体赋值,在我写的这个例子中,源实体中包含的实体类字段为Sub,里面包含的字段名字为Age,
那么destmodel中对应的字段名字可以是:SubAge,那么automapper就可以自动为你赋值了,大家看最后的运行结果。
给大家看下我建的源实体:
public class Source1{public string Name { set; get; }public string GetA { set; get; }public string GetD { set; get; }public string SetB { set; get; }public string c { set; get; }public SubSource1 Sub { set; get; }}public class SubSource1{public string Age { set; get; }}
还有viewmodel(要转化成为你想要的模型):
public class Dest1{public string Name { set; get; }public string A { set; get; }public string C { set; get; }public string SubAge { set; get; }public string D { set; get; }}
我封装的扩展方法:
/// <summary>/// 类型映射,默认字段名字一一对应/// </summary>/// <typeparam name="TDestination">转化之后的model,可以理解为viewmodel</typeparam>/// <typeparam name="TSource">要被转化的实体,Entity</typeparam>/// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param>/// <returns>转化之后的实体</returns>public static TDestination MapTo<TDestination, TSource>(this TSource source)where TDestination : classwhere TSource : class{if (source == null) return default(TDestination);var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());var mapper = config.CreateMapper();return mapper.Map<TDestination>(source);}
使用方式:
var source1 = new Source1{Name = "source",Sub = new SubSource1 { Age = "25" },c = "c",GetA = "A",SetB = "B"};var destViewModel = source1.MapTo<Source1,Dest1>();
运行结果:
2.viewmodel与实体字段名字没有全部对应,只有几个字段的名字和源实体中的字段名字是一样的,其他的字段是通过实体中的几个字段组合或者是格式或者是类型转化而来的,
使用方法:不能再使用这个扩展方法了,只能自己额外写代码,代码如下:
var config2 = new MapperConfiguration(cfg => cfg.CreateMap<SourceUser, DestUser2>().ForMember(d => d.DestName, opt => opt.MapFrom(s => s.Name)) //指定字段一一对应.ForMember(d => d.Birthday, opt => opt.MapFrom(src => src.Birthday.ToString("yy-MM-dd HH:mm")))//指定字段,并转化指定的格式.ForMember(d => d.Age, opt => opt.Condition(src => src.Age > 5))//条件赋值.ForMember(d => d.A1, opt => opt.Ignore())//忽略该字段,不给该字段赋值.ForMember(d => d.A1, opt => opt.NullSubstitute("Default Value"))//如果源字段值为空,则赋值为 Default Value.ForMember(d => d.A1, opt => opt.MapFrom(src => src.Name + src.Age * 3 + src.Birthday.ToString("d"))));//可以自己随意组合赋值var mapper2 = config2.CreateMapper();
注释中都包含了平时常用的几种情况,其他的我就没有再写。
下面再给大家把list转化的扩展方法代码贴上:
/// <summary>/// 集合列表类型映射,默认字段名字一一对应/// </summary>/// <typeparam name="TDestination">转化之后的model,可以理解为viewmodel</typeparam>/// <typeparam name="TSource">要被转化的实体,Entity</typeparam>/// <param name="source">可以使用这个扩展方法的类型,任何引用类型</param>/// <returns>转化之后的实体列表</returns>public static IEnumerable<TDestination> MapToList<TSource,TDestination>(this IEnumerable<TSource> source)where TDestination : classwhere TSource : class{if (source == null) return new List<TDestination>();var config = new MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());var mapper = config.CreateMapper();return mapper.Map<List<TDestination>>(source);}
同样的使用方式:
var source1 = new Source1{Name = "source",Sub = new SubSource1 { Age = "25" },c = "c",GetA = "A",SetB = "B"};var source3 = new Source1{Name = "source3",Sub = new SubSource1 { Age = "253" },c = "c3",GetA = "A3",SetB = "B3"};var sourceList = new List<Source1> { source1, source3 };var destViewModelList = sourceList.MapToList<Source1,Dest1>();
运行结果:
以上就是我个人所得,如有错误,欢迎大家指正。
修改:destination和source写反了,导致我的总结有些错误,现在纠正一下:错误结论已经红色标注,中间的截图也换成正确的了,工具类方法也已经修正。
- 本文作者:GeekPower - Felix Sun
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!
