• 封装一个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
  • 相关阅读:
    (转)干货|一次完整的性能测试,测试人员需要做什么?
    (转)JMeter性能测试-服务器资源监控插件详解
    【Android Apk重新签名报错re-sign.jar之解决方法】
    CrackMe_001
    判断二叉树是否是镜像对称
    顺时针打印矩阵
    利用前序遍历和中序遍历构造二叉树
    二叉树的四种遍历方式
    最长回文子串
    同步/异步/阻塞/非阻塞
  • 原文地址:https://www.cnblogs.com/zzlblog/p/10013182.html
Copyright © 2020-2023  润新知