Mapster工具类
一、使用Mapster
//安装nuget包 Install-Package Mapster
二、封装工具类
/// <summary> /// Mapster映射帮助类 -基础使用 /// Mapster 版本7.3.0(或7.2.0) /// ASP.NET使用时可直接services.AddMapster(); /// </summary> public class MapsterHelper { #region 实体映射 /// <summary> /// 1.1、类型映射_默认字段一一对应 /// T需要映射后的实体 = 需要映射的实体.Adapt<T需要映射后的实体>(); /// </summary> /// <typeparam name="TSource">源类型</typeparam> /// <typeparam name="TDestination">目标类型</typeparam> /// <param name="tSource">源数据</param> /// <returns></returns> public static TDestination MapsterTo<TSource, TDestination>(TSource tSource) where TSource : class where TDestination : class { if (tSource == null) return default; return tSource.Adapt<TDestination>(); } /// <summary> /// 1.2、类型映射_默认字段一一对应 (映射到现有对象) /// T需要映射后的实体 = 需要映射的实体.Adapt<T需要映射后的实体>(); /// </summary> /// <typeparam name="TSource">源类型</typeparam> /// <typeparam name="TDestination">目标类型</typeparam> /// <param name="tDestination">目标对象</param> /// <param name="tSource">源数据</param> /// <returns></returns> public static TDestination MapsterTo<TSource, TDestination>(TDestination tDestination, TSource tSource) where TSource : class where TDestination : class { if (tSource == null) return default; return tSource.Adapt(tDestination); } /// <summary> /// 2、类型映射 /// ① 字段名称不对应 /// ② 类型转化 /// ③ 字段省略 /// ④ 字段名称或类型不对应 /// ⑤ 条件赋值或null处理 /// ⑥ 组合赋值 /// </summary> /// <typeparam name="TSource">源类型</typeparam> /// <typeparam name="TDestination">目标类型</typeparam> /// <param name="tSource">源数据</param> /// <param name="configurationExpression">类型</param> /// <returns></returns> public static TDestination MapsterTo<TSource, TDestination>(TSource tSource, TypeAdapterConfig typeAdapterConfig) where TSource : class where TDestination : class { if (tSource == null) return default; //var typeAdapterConfig = new TypeAdapterConfig(); //typeAdapterConfig.ForType<MapsterTestTable_ViewModel, MapsterTestTable>() // .Map(member => member.DestName, source => source.Name) // 指定字段一一对应 // .Map(member => member.Birthday, source => source.Birthday.ToString("yy-MM-dd HH:mm")) // 指定字段,并转化指定的格式 // .Map(member => member.Age, source => source.Age > 5) // 条件赋值 // .Ignore(member => member.A1) // 忽略该字段,不给该字段赋值 // .IgnoreNullValues(true) // 忽略空值映射 // .IgnoreAttribute(typeof(DataMemberAttribute)) // 忽略指定特性的字段 // .Map(member => member.A3, source => source.Name + source.Age * 3 + source.Birthday.ToString("d")) // 可以自己随意组合赋值 // .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase); // 忽略字段名称的大小写 var mapper = new Mapper(typeAdapterConfig); // 可以自己随意组合赋值 return mapper.Map<TDestination>(tSource); } #endregion 实体映射 #region 列表映射 /// <summary> /// 3、集合列表类型映射,默认字段名字一一对应 /// </summary> /// <typeparam name="TSource">源类型</typeparam> /// <typeparam name="TDestination">目标类型</typeparam> /// <param name="source">源数据</param> /// <returns></returns> public static List<TDestination> MapsterToList<TSource, TDestination>(List<TSource> sources) where TSource : class where TDestination : class { if (sources == null) return new List<TDestination>(); return sources.Adapt<List<TDestination>>(); } /// <summary> /// 3、集合列表类型映射,默认字段名字一一对应 /// </summary> /// <typeparam name="TSource">源类型</typeparam> /// <typeparam name="TDestination">目标类型</typeparam> /// <param name="source">源数据</param> /// <returns></returns> public static List<TDestination> MapsterToList<TSource, TDestination>(List<TSource> sources, TypeAdapterConfig typeAdapterConfig) where TSource : class where TDestination : class { if (sources == null) return new List<TDestination>(); var mapper = new Mapper(typeAdapterConfig); // 可以自己随意组合赋值 return mapper.Map<List<TDestination>>(sources); } #endregion 列表映射 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!