(更新时间)2021年5月31日 商城高并发秒杀系统(.NET Core版) 16-AutoMapper帮助类的封装
【摘要】
一:AutoMapper帮助类的封装
/// <summary>
/// 对象自动转换工具类
/// </summary>
public static class AutoMap...
一:AutoMapper帮助类的封装
/// <summary>
/// 对象自动转换工具类
/// </summary>
public static class AutoMapperHelper
{
/// <summary>
/// 类型映射,默认字段名字一一对应
/// </summary>
/// <typeparam name="TDestination"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static TDestination AutoMapTo<TDestination>(this object obj)
{
if (obj == null) return default(TDestination);
var config = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap(obj.GetType(), typeof(TDestination)));
return config.CreateMapper().Map<TDestination>(obj);
}
/// <summary>
/// 类型映射,可指定映射字段的配置信息
/// </summary>
/// <typeparam name="TSource">源数据:要被转化的实体对象</typeparam>
/// <typeparam name="TDestination">目标数据:转换后的实体对象</typeparam>
/// <param name="source">任何引用类型对象</param>
/// <param name="cfgExp">可为null,则自动一一映射</param>
/// <returns></returns>
public static TDestination AutoMapTo<TSource, TDestination>(this TSource source, Action<AutoMapper.IMapperConfigurationExpression> cfgExp)
where TDestination : class
where TSource : class
{
if (source == null) return default(TDestination);
var config = new AutoMapper.MapperConfiguration(cfgExp != null ? cfgExp : cfg => cfg.CreateMap<TSource, TDestination>());
var mapper = config.CreateMapper();
return mapper.Map<TDestination>(source);
}
/// <summary>
/// 类型映射,默认字段名字一一对应
/// </summary>
/// <typeparam name="TSource">源数据:要被转化的实体对象</typeparam>
/// <typeparam name="TDestination">目标数据:转换后的实体对象</typeparam>
/// <param name="source">任何引用类型对象</param>
/// <returns>转化之后的实体</returns>
public static TDestination AutoMapTo<TSource, TDestination>(this TSource source)
where TDestination : class
where TSource : class
{
if (source == null) return default(TDestination);
var config = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
var mapper = config.CreateMapper();
return mapper.Map<TDestination>(source);
}
/// <summary>
/// 集合列表类型映射,默认字段名字一一对应
/// </summary>
/// <typeparam name="TDestination">转化之后的实体对象,可以理解为viewmodel</typeparam>
/// <typeparam name="TSource">要被转化的实体对象,Entity</typeparam>
/// <param name="source">通过泛型指定的这个扩展方法的类型,理论任何引用类型</param>
/// <returns>转化之后的实体列表</returns>
public static IEnumerable<TDestination> AutoMapTo<TSource, TDestination>(this IEnumerable<TSource> source)
where TDestination : class
where TSource : class
{
if (source == null) return new List<TDestination>();
var config = new AutoMapper.MapperConfiguration(cfg => cfg.CreateMap<TSource, TDestination>());
var mapper = config.CreateMapper();
return mapper.Map<List<TDestination>>(source);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/117409061
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)