码迷,mamicode.com
首页 > 移动开发 > 详细

封装一个Automapper单例

时间:2018-11-24 20:58:33      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:ado   属性   color   span   ret   volatile   readonly   ever   一个   

  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

 

封装一个Automapper单例

标签:ado   属性   color   span   ret   volatile   readonly   ever   一个   

原文地址:https://www.cnblogs.com/zzlblog/p/10013182.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!