.Net_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 列表映射


}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值