一、Mapster简介

1、什么是Mapster(官方文档地址:https://github.com/MapsterMapper/Mapster)

  Mapster是一个高性能的用于对象映射的类库,同类型的产品还有AutoMapper。它提供了一系列的API和工具,以下为几个重要的类和接口:

  • @Adapt注解:用于将实体类映射到数据库表中的指定列上。
  • @Mapper注解:用于将实体类映射到数据库表中的指定列上。
  • Entity接口:定义了实体类的属性和映射规则。
  • Mapper接口:定义了映射规则的具体实现。
  • Query接口:定义了查询语句的规则。
  • DataStore接口:定义了数据存储的接口。
2、Mapster与AutoMapper的性能对比:

方法

平均值

标准偏差

错误

第0代

第1代

第2代

分配

“Mapster 6.0.0”

108.59毫秒

1.198毫秒

1.811毫秒

31000

-

-

124.36 MB

“Mapster 6.0.0(罗斯林)”

38.45毫秒

0.494毫秒

0.830毫秒

31142.8571

-

-

124.36 MB

'Mapster 6.0.0(FEC)'

37.03毫秒

0.281毫秒

0.472毫秒

29642.8571

-

-

118.26 MB

'Mapster 6.0.0(Codegen)'

34.16毫秒

0.209毫秒

0.316毫秒

31133.3333

-

-

124.36 MB

“ExpressMapper 1.9.1”

205.78毫秒

5.357毫秒

8.098毫秒

59000

-

-

236.51 MB

“AutoMapper 10.0.0”

420.97毫秒

23.266毫秒

35.174毫秒

87000

-

-

350.95 MB

二、Mapster帮助类

/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描    述:Mapster映射帮助类-基础使用 (Mapster 版本7.3.0或7.2.0)                                              
*│ 作    者:执笔小白                                              
*│ 版    本:1.0                                       
*│ 创建时间:2023-06-28 15:01:11                            
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Util.ObjMapperUtil                               
*│ 类    名:MapsterHelper                                    
*└──────────────────────────────────────────────────────────────┘
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Threading.Tasks;
using Mapster;
using MapsterMapper;

namespace fly_webapi.Util.ObjMapperUtil
{
    /// <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>>();
        }
        #endregion 列表映射

        
    }

    /// <summary>
    /// Mapster映射帮助类 - 示例(未测试)
    /// Mapster 版本12.0.0
    /// </summary>
    public class MapsterHelperTeatDemo
    {
        #region 实体映射
        /// <summary>
        /// 1、类型映射_默认字段一一对应
        /// </summary>
        public void MapsterToDemo()
        {
            MapsterTestTable_ViewModel tableTest1 = new();

            MapsterTestTable tableTest = MapsterHelper.MapsterTo<MapsterTestTable_ViewModel, MapsterTestTable>(tableTest1);
        }

        /// <summary>
        /// 2、类型映射
        /// ① 字段名称不对应
        /// ② 类型转化
        /// ③ 字段省略
        /// ④ 字段名称或类型不对应
        /// ⑤ 条件赋值或null处理
        /// ⑥ 组合赋值
        /// </summary>
        public void MapsterTo2Demo()
        {
            MapsterTestTable_ViewModel tableTest1 = new();
            // 这里是语句,不是无用的数据;
            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);                                                            // 忽略字段名称的大小写

            MapsterTestTable tableTest = MapsterHelper.MapsterTo<MapsterTestTable_ViewModel, MapsterTestTable>(tableTest1, typeAdapterConfig);
        }
        #endregion 实体映射

        #region 列表映射
        /// <summary>
        /// 3、集合列表类型映射,默认字段名字一一对应
        /// </summary>
        public void MapsterToListDemo()
        {
            List<MapsterTestTable_ViewModel> tableTestVMs = new();

            List<MapsterTestTable> tableTests = MapsterHelper.MapsterToList<MapsterTestTable_ViewModel, MapsterTestTable>(tableTestVMs);
        }
        #endregion 列表映射
    }

    #region MapsterHelper测试示例用
    public class MapsterTestTable_ViewModel
    {
        public string Name { get; set; }
        public DateTime Birthday { get; set; }
        public int Age { get; set; }

        public DateTime A4 { get; set; }
    }

    // [AdaptTo("MapsterTestTable_ViewModel"), GenerateMapper]  // 显示声明式映射写法
    public class MapsterTestTable
    {
        public string DestName { get; set; }
        public DateTime Birthday { get; set; }
        public int Age { get; set; }

        //[AdaptIgnore]  // 显示声明式忽略该字段
        public string A1 { get; set; }
        public string A2 { get; set; }
        public string A3 { get; set; }
    }
    #endregion MapsterHelper测试示例用
}
  • 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.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.

\

 

作者:꧁执笔小白꧂