• 封装一个Automapper单例


      public class DataModule : IModule
        {
            public void Configure(IMapperConfigurationExpression cfg)
            {
                //cfg.CreateMap<ApproveAtcPo, ApproveAtcVo>().ReverseMap();
           
            }
        }
             /// <summary>
            /// 注册需要转换的类型
            /// </summary>
            private PoMapper()
            {
                AutoMapper.Mapper.Initialize(cfg =>
                {
    //多模块注册方式
    new DataModule().Configure(cfg); new AliasModule().Configure(cfg); }); }
     1     public class MapperTool
     2     {
     3         private static volatile MapperTool mapper = null;
     4         private static object syncRoot = new Object();
     5         public static readonly List<Type> typeList = null;
     6         /// <summary>
     7         /// 注册需要转换的类型
     8         /// </summary>
     9         private MapperTool()
    10         {
    11             Mapper.Initialize(cfg =>
    12             {
    13                 //指定不同属性映射demo
    14                 //cfg.CreateMap<auth_userVo, t_auth_user>()
    15                 //   .ReverseMap()
    16                 //   .ForMember(dest => dest.id, opt => opt.MapFrom(src => src.rid))
    17                 //   .ForMember(dest => dest.update, opt => opt.MapFrom(src => src.create));
    18              
    19             });
    20             //验证是否所有字段都转换了
    21             //Mapper.Configuration.AssertConfigurationIsValid();
    22         }
    23 
    24         private MapperTool(List<Type[]> TypeList)
    25         {
    26             Mapper.Initialize(cfg =>
    27             {
    28                 foreach (var type in TypeList)
    29                 {
    30                     cfg.CreateMap(type[0], type[1]).ReverseMap();
    31                 }
    32             });
    33         }
    34 
    35 
    36         /// <summary>
    37         /// 初始化注册Mapper
    38         /// </summary>
    39         public static MapperTool Instance
    40         {
    41             get
    42             {
    43                 if (mapper == null)
    44                 {
    45                     lock (syncRoot)
    46                     {
    47                         if (mapper == null)
    48                         {
    49                             mapper = new MapperTool();
    50                         }
    51 
    52                     }
    53                 }
    54                 return mapper;
    55             }
    56         }
    57 
    58         /// <summary>
    59         /// 传入需要转换的对象
    60         /// </summary>
    61         /// <typeparam name="F">需要转换的对象类型</typeparam>
    62         /// <typeparam name="T">转换目标对象类型</typeparam>
    63         /// <param name="f">需要转换的对象</param>
    64         /// <returns>目标对象</returns>
    65         public T Map<F, T>(F f)
    66             where F : new()
    67             where T : new()
    68         {
    69             return Mapper.Map<F, T>(f);
    70         }
    71     }
    View Code
  • 相关阅读:
    第三周学习进度
    计算最低价格
    第二阶段冲刺5
    第二阶段冲刺4
    第十三周进度条
    第二阶段冲刺3
    寻找小水王
    第二阶段冲刺2
    第二阶段冲刺1
    构建之法阅读笔记
  • 原文地址:https://www.cnblogs.com/zzlblog/p/10013182.html
Copyright © 2020-2023  润新知