封装一个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

 

转载于:https://www.cnblogs.com/zzlblog/p/10013182.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值