自己造轮子系列之OOM框架AutoMapper(记一次代码优化->ExpressionTree)

【前言】

OOM框架想必大家在Web开发中是使用频率非常之高的,如果还不甚了解OOM框架,那么我们对OOM框架稍作讲解。

OOM顾名思义,Object-Object-Mapping实体间相互转换。常见的使用场景有两个实体要通过DTO对象进行页面的渲染,那么我们就需要通过对DTO对象的一个一个属性进行赋值,最终返回。整个过程是单调又繁琐的,甚至严重影响了代码的整洁性。更有强迫症高度患者可能看着这一坨shi一样的代码阵阵痉挛...因此,我们就想,能不能用一种简介的方法进行自动映射两个,三个甚至更多的对象,避免写这么一些低级乏味的代码。

于是乎,一大波一大波的自动映射框架悠然而生。业内比较出名的有:AutoMapper,EmitMapper,NLiteMapper,TinyMapper等。在这里,先抛去他们不谈,毕竟未必个人觉得好用,或者我就喜欢造轮子。本篇将讲解一波自己造的轮子OOM框架AutoMapper,并且有造轮子过程中的持续优化,我想,这便是学习的最好过程吧。

本文讲解了反射方式的实现方法以及后续抛弃反射的优化方法Expression Tree 表达式树的实现方法。

【实现思路】

  AutoMapper既然是OOM框架,那么就离不开Object,Object必然又涉及到类型。那么,泛型是必不可少的。

  框架要简洁易用,并且能兼容多数场景。于是,我们准备从以下几方面进行设计:

  1. 支持自动映射,能通过调用一个方法自动映射实体;
  2. 支持特殊属性特殊处理的扩展功能,特殊处理的部分可以特殊进行赋值;
  3. 有不想进行映射的属性能够进行屏蔽;
  4. 映射的名称要支持配置(有名字不同的场景无法自动映射,手动配置映射关系进行映射);
  5. 配置不能加配置文件,通过标签的方式(约定优于配置);
  6. 性能要卓越;
  7. 代码要通用,版本支持度要高(使用.net standard类库,可同时支持.net framework 和 .netcore);

【实现过程】

  项目结构:

  自己造轮子系列之OOM框架AutoMapper(记一次代码优化->ExpressionTree)_第1张图片

  依据上面的思路,第一时间想到的便是通过反射获取到第一个对象的属性值,并动态创建第二个对象,遍历属性赋值,再添加细节处理。SoEasy!于是便兴冲冲完成了第一版。

  首先构造了几个特性标签类,用于标注是否要映射,以及映射的自定义别名的配置。

  DoNotMapperAttribute:不进行映射该属性

  MapperAttribute:添加自定义名称的映射配置

  MapperClassAttribute:该类支持映射(目前没有实际应用)

1 using System;
2 
3 namespace SevenTiny.Bantina.AutoMapper
4 {
5     [AttributeUsage(AttributeTargets.Property, Inherited = true)]
6     public class DoNotMapperAttribute :Attribute
7     {
8     }
9 }
 1 using System;
 2 using System.Linq;
 3 using System.Reflection;
 4 
 5 namespace SevenTiny.Bantina.AutoMapper
 6 {
 7     [AttributeUsage(AttributeTargets.Property, Inherited = true)]
 8     public class MapperAttribute : Attribute
 9     {
10         public string TargetName { get; set; }
11 
12         public MapperAttribute() { }
13         public MapperAttribute(string targetName)
14         {
15             this.TargetName = targetName;
16         }
17 
18         public static string GetTargetName(PropertyInfo property)
19         {
20             var attr = property.GetCustomAttributes(true).FirstOrDefault();
21             return attr != null ? (attr as MapperAttribute).TargetName ?? default(string) : default(string);
22         }
23     }
24 }
 1 using System;
 2 using System.Linq;
 3 
 4 namespace SevenTiny.Bantina.AutoMapper
 5 {
 6     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field, Inherited = true)]
 7     public class MapperClassAttribute : Attribute
 8     {
 9         public string Name { get; set; }
10         public static string GetName(Type type)
11         {
12             var attr = type.GetCustomAttributes(typeof(MapperClassAttribute), true).FirstOrDefault();
13             return attr != null ? (attr as MapperClassAttribute).Name ?? default(string) : default(string);
14         }
15     }
16 } 

第一版反射实现代码:

 1 public sealed class Mapper
 2     {
 3         private Mapper() { }
 4         /// 
 5         /// Init Source Value Dic
 6         /// 
 7         /// 
 8         /// 
 9         /// 
10         private static Dictionary<string, object> Initdic(TSource source)
11         {
12             Dictionary<string, object> dic = new Dictionary<string, object>();
13             foreach (PropertyInfo property in typeof(TSource).GetProperties())
14             {
15                 string targetPropertyName = MapperAttribute.GetTargetName(property);
16                 if (!string.IsNullOrEmpty(targetPropertyName))
17                 {
18                     if (!dic.ContainsKey(targetPropertyName))
19                     {
20                         dic.Add(targetPropertyName, property.GetValue(source));
21                     }
22                 }
23                 else if (!dic.ContainsKey(property.Name))
24                 {
25                     dic.Add(property.Name, property.GetValue(source));
26                 }
27             }
28             return dic;
29         }
30         /// 
31         /// SetValue from propertyinfo and sourceDictionary
32         /// 
33         /// 
34         /// 
35         /// 
36         /// 
37         /// 
38         private static TValue SetValue(TValue value, PropertyInfo[] propertyInfos, Dictionary<string, object> sourceDic, Dictionary<string, string> keys) where TValue : class
39         {
40             foreach (PropertyInfo property in propertyInfos)
41             {
42                 if (!keys.ContainsKey(property.Name))
43                 {
44                     if (sourceDic.ContainsKey(property.Name))
45                     {
46                         try
47                         {
48                             property.SetValue(value, sourceDic[property.Name]);
49                             keys.Add(property.Name, string.Empty);
50                         }
51                         catch (Exception)
52                         {
53                             property.SetValue(value, null);
54                         }
55                     }
56                 }
57             }
58             return value;
59         }
60         /// 
61         /// AutoMapper
62         /// 
63         /// value type
64         /// source type
65         /// 
66         /// 
67         public static TValue AutoMapper(TSource source) where TValue : class where TSource : class
68         {
69             TValue value = Activator.CreateInstance();
70             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
71             Dictionary<string, string> keys = new Dictionary<string, string>();
72             value = SetValue(value, propertyInfos, Initdic(source), keys);
73             return value;
74         }
75         /// 
76         /// AutoMapper,Support for Use Action to custom special fields.
77         /// 
78         /// 
79         /// 
80         /// 
81         /// 
82         /// 
83         public static TValue AutoMapper(TSource source, Action action) where TValue : class where TSource : class
84         {
85             TValue value = Activator.CreateInstance();
86             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
87             Dictionary<string, string> keys = new Dictionary<string, string>();
88             value = SetValue(value, propertyInfos, Initdic(source), keys);
89             action(value);
90             return value;
91         }
92     }

由于思路清晰,实现起来也是很容易的。

通过传入两个泛型(一个输入,一个输出),然后获取到其中一个的属性,并遍历另一个的属性,如果相同,则直接赋值(当然有特性标签部分的细节处理)。

由于有些属性已经赋值过了,再次遇到会重新扫描赋值,降低了性能和影响了逻辑性,这里做了Dictionary的暂时缓存,以便扫描过的值不再进行赋值。

特殊的值,我们使用了Action action 匿名委托的方式进行赋值,提供了特殊处理的能力。

我们下面进行一波使用,简单封装一个多次调用的测试方法:

 1 public static TimeSpan Caculate(int executTimes, Action action)
 2 {
 3    Stopwatch sw = new Stopwatch();
 4    sw.Start();
 5    for (int i = 0; i < executTimes; i++)
 6    {
 7      action();
 8    }
 9    sw.Stop();
10    return sw.Elapsed;
11 }

简单写一个控制台应用程序,并传入参数100万次调用,并打印执行用时:

1 Student1 stu1 = new Student1 { Uid = Guid.NewGuid() };
2  Student5 stu5 = new Student5 { HealthLevel = 100, SchoolClass = new SchoolClass { Name = "class1" } };
3 
4  var test1 = StopwatchHelper.Caculate(1000000, () =>
5  {
6     Student stu = Mapper.AutoMapper(stu5, t => t.Name = "jony");
7  });
8  Console.WriteLine(test1.TotalMilliseconds);

自己造轮子系列之OOM框架AutoMapper(记一次代码优化->ExpressionTree)_第2张图片

执行100w次大概需要3654毫秒,也就是3.6秒。

虽说100万次小程序是可以忽略的,但是对于高并发的场景100万次再平常不过了,这个性能是不能忍受的!

再看一眼直接调用的性能:

1 Student5 stu5 = new Student5 { HealthLevel = 100, SchoolClass = new SchoolClass { Name = "class1" } };
2 
3 var test1 = StopwatchHelper.Caculate(1000000, () =>
4 {
5     Student stu = new Student { HealthLevel = stu5.HealthLevel, SchoolClass = stu5.SchoolClass };
6 });
7 Console.WriteLine(test1.TotalMilliseconds);

自己造轮子系列之OOM框架AutoMapper(记一次代码优化->ExpressionTree)_第3张图片

直接最简单New对象赋值的操作耗时是100万次34毫秒,也就是0.034秒的样子差距还是挺大的。

可见这样的性能是注定不可能成为一个好用的组件的!更别谈高性能了。

于是便开始了在茫茫的代码库中淘取黑科技的一波操作。很感谢现代的搜索引擎,让我能在迷失的黑夜中找到一盏明灯。

反射造成的性能问题是我迫切要解决的,反射性能的规避通常有以下几种方式进行提升:

  1. 缓存,高并发场景下绝对是要避免每次都去调用反射代码的,将反射部分尽量缓存下来。
  2. 调用C/C++代码库,但是首先要懂C/C++,其次要保证调用dll的时间要将反射的时间换取回来。
  3. 通过Expression Tree的方式构造表达式树,然后将表达式树缓存起来,这样在之后调用的代码都是等价于直接执行代码的。
  4. 通过IL Emit的方式动态构造代码,更为底层的IL代码提供更高的效率。

在这四种方案中,我聚焦到了最后两种上。

Activator.CreateInstance()

因为反射即便再怎么缓存,上述构建新实例的方法也是很耗费性能的(更别说由于临时工赶.net类库的问题,哈哈哈,太搞笑,可以参考此博文跟随老一辈程序员关于该方法性能的探讨以及源码的剖析http://www.cnblogs.com/leven/archive/2009/12/08/instanse_create_comparison.html),其次还有通过属性去获取值。这些都是难以通过缓存解决的。

IL Emit的实现方式是比较复杂的,稍一不慎,还容易造成内存泄漏等严重的问题。让我一个半吊子程序员来写这么细致的代码,暂时选择回避。

我选择了第三种:通过Expression Tree的方式构造表达式树,并且缓存委托方法进行调用的方法。表达式树这里就不进行详细讲解了,会放在其他博文里面单独讲解(也是一大块学问哦)。

和第一版大同小异,也是通过泛型的思想进行构建。不同的是泛型从方法上传递改为了类上传递,这也是由于缓存Func部分的泛型难以直接传递转换的方式,这个对使用并无太大影响。

第二版 Expression Tree 表达式树方式实现代码:

为了代码通用,抽取了公共部分构造了一个内部的类进行对表达式树的构建操作:

 1 /*********************************************************
 2  * CopyRight: 7TINY CODE BUILDER. 
 3  * Version: 5.0.0
 4  * Author: 7tiny
 5  * Address: Earth
 6  * Create: 2018-04-09 16:55:16
 7  * Modify: 2018-04-09 16:55:16
 8  * E-mail: [email protected] | [email protected] 
 9  * GitHub: https://github.com/sevenTiny 
10  * Personal web site: http://www.7tiny.com 
11  * Technical WebSit: http://www.cnblogs.com/7tiny/ 
12  * Description: 
13  * Thx , Best Regards ~
14  *********************************************************/
15 using System;
16 using System.Collections.Generic;
17 using System.Linq;
18 using System.Linq.Expressions;
19 using System.Reflection;
20 
21 namespace SevenTiny.Bantina.AutoMapper
22 {
23     internal sealed class MapperExpressionCommon
24     {
25         /// 
26         /// structure func
27         /// 
28         /// 
29         /// 
30         /// 
31         /// 
32         public static void GetFunc(Type outType, Type[] inTypes, out MemberInitExpression memberInitExpression, out List parameterExpressionList)
33         {
34             parameterExpressionList = new List();
35             List memberBindingList = new List();
36             PropertyInfo[] propertyInfos = outType.GetProperties();
37             Dictionary<string, PropertyInfo> outPropertyDic = propertyInfos.ToDictionary(t => t.Name, t => t);
38             foreach (var inType in inTypes)
39             {
40                 ParameterExpression parameterExpression = Expression.Parameter(inType, inType.FullName);
41                 PropertyInfo[] inTypePpropertyInfos = inType.GetProperties();
42                 foreach (var inTypeInfo in inTypePpropertyInfos)
43                 {
44                     if (inTypeInfo.GetCustomAttribute(typeof(DoNotMapperAttribute)) == null)
45                     {
46                         //first
47                         string outPropertyDicKey = MapperAttribute.GetTargetName(inTypeInfo);
48                         //second
49                         if (string.IsNullOrEmpty(outPropertyDicKey) && outPropertyDic.Keys.Contains(inTypeInfo.Name))
50                         {
51                             outPropertyDicKey = inTypeInfo.Name;
52                         }
53                         //third
54                         if (!string.IsNullOrEmpty(outPropertyDicKey) && outPropertyDic.Keys.Contains(outPropertyDicKey))
55                         {
56                             MemberExpression property = Expression.Property(parameterExpression, inTypeInfo);
57                             MemberBinding memberBinding = Expression.Bind(outPropertyDic[outPropertyDicKey], property);
58                             memberBindingList.Add(memberBinding);
59                             outPropertyDic.Remove(outPropertyDicKey);//remove property if has be valued
60                         }
61                     }
62                 }
63                 if (!parameterExpressionList.Exists(t => t.Name.Equals(parameterExpression.Name)))
64                 {
65                     parameterExpressionList.Add(parameterExpression);
66                 }
67             }
68             memberInitExpression = Expression.MemberInit(Expression.New(outType), memberBindingList.ToArray());
69         }
70     }
71 
72 }

在上面的这段代码中:

 ParameterExpression parameterExpression = Expression.Parameter(inType, inType.FullName); 

构建了类似 t 的结构。

MemberExpression property = Expression.Property(parameterExpression, inTypeInfo);
MemberBinding memberBinding = Expression.Bind(outPropertyDic[outPropertyDicKey], property);

构建了类似 HealthLevel = t.HealthLevel 的结构。

 1 memberInitExpression = Expression.MemberInit(Expression.New(outType), memberBindingList.ToArray()); 

构建了类似 new Student() {HealthLevel = t.HealthLevel, SchoolClass = t.SchoolClass} 的结构。

这段逻辑代码的调用方代码:

 1 /*********************************************************
 2  * CopyRight: 7TINY CODE BUILDER. 
 3  * Version: 5.0.0
 4  * Author: 7tiny
 5  * Address: Earth
 6  * Create: 2018-03-16 10:11:43
 7  * Modify: 2018-4-3 11:35:53
 8  * E-mail: [email protected] | [email protected] 
 9  * GitHub: https://github.com/sevenTiny 
10  * Personal web site: http://www.7tiny.com 
11  * Technical WebSit: http://www.cnblogs.com/7tiny/ 
12  * Description: 
13  * Thx , Best Regards ~
14  *********************************************************/
15 using System;
16 using System.Collections.Generic;
17 using System.Linq;
18 using System.Linq.Expressions;
19 using System.Reflection;
20 
21 namespace SevenTiny.Bantina.AutoMapper
22 {
23     public sealed class Mapper where TOut : class where TIn : class
24     {
25         private Mapper() { }
26         private static readonly Func funcCache = GetFunc();
27         public static TOut AutoMapper(TIn tIn)
28         {
29             return funcCache(tIn);
30         }
31         public static TOut AutoMapper(TIn tIn, Action action)
32         {
33             TOut outValue = funcCache(tIn);
34             action(outValue);
35             return outValue;
36         }
37         private static Func GetFunc()
38         {
39             Type[] types = new Type[] { typeof(TIn) };
40             MemberInitExpression memberInitExpression;
41             List parameterExpressionList;
42             MapperExpressionCommon.GetFunc(typeof(TOut), types, out memberInitExpression, out parameterExpressionList);
43             Expression> lambda = Expression.Lambda>(memberInitExpression, parameterExpressionList);
44             return lambda.Compile();
45         }
46     }
47 }

在上面这段代码中:

Expression> lambda = Expression.Lambda>(memberInitExpression, parameterExpressionList);

使用上面的公共方法out出的参数构建了一个完整的lambda表达式  t => new Student() {HealthLevel = t.HealthLevel, SchoolClass = t.SchoolClass}

从代码断点中也可以看出:

这里的t由于公共方法中使用的是 type(T).FullName,所以看起来比较长,是Test.SevenTiny.Bantina.Model.Student5,可以看成是一个小写的“t”,这里是等效的。

lambda.Compile()

将上述的表达式执行为Func 的一个匿名委托。

private static readonly Func funcCache = GetFunc();

将执行后的结果(匿名委托)缓存起来。

这样最终的执行结果便转换成了一段代码:

Func stuFunc = t=>new Student() {HealthLevel = t.HealthLevel, SchoolClass = t.SchoolClass};

调用这段代码和直接实例化方法赋值是等效的,但是调用这段代码可以将很多通用的方法封装成一个公共的方法,提高了代码的重用性。

这段代码耗时的部分便是通过一定的反射构建出一个Func所耗费的时间,这在数百万次的调用中仅仅调用了一次,而执行部分却是不耗费时间的,在高并发的场景下占有很大的优势。

下面我们实际测试一下:

同样的代码,使用了第二版的AutoMapper,执行100万次

1 Student5 stu5 = new Student5 { HealthLevel = 100, SchoolClass = new SchoolClass { Name = "class1" } };
2 
3 var test1 = StopwatchHelper.Caculate(1000000, () =>
4 {
5     Student stu = Mapper.AutoMapper(stu5, t => t.Name = "jony");
6 });
7 Console.WriteLine(test1.TotalMilliseconds);

自己造轮子系列之OOM框架AutoMapper(记一次代码优化->ExpressionTree)_第4张图片

 我们加大调用的次数再次进行比较:

 1 Student5 stu5 = new Student5 { HealthLevel = 100, SchoolClass = new SchoolClass { Name = "class1" } };
 2 
 3 var test0 = StopwatchHelper.Caculate(1000000, () =>
 4 {
 5     Student stu = Mapper.AutoMapper(stu5, t => t.Name = "jony");
 6 });
 7 Console.WriteLine("使用反射调用 1 百万次耗时:");
 8 Console.WriteLine(test0.TotalMilliseconds);
 9 
10 Console.WriteLine();
11 
12 var test1 = StopwatchHelper.Caculate(1000000, () =>
13 {
14     Student stu = Mapper.AutoMapper(stu5, t => t.Name = "jony");
15 });
16 Console.WriteLine("使用Expression表达式树调用 1 百万次耗时:");
17 Console.WriteLine(test1.TotalMilliseconds);
18 
19 Console.WriteLine();
20 
21 var test2 = StopwatchHelper.Caculate(1000000, () =>
22 {
23     Student stu = new Student { HealthLevel = stu5.HealthLevel, Name = "jony" };
24 });
25 Console.WriteLine("使用代码直接构建 1 百万次耗时:");
26 Console.WriteLine(test2.TotalMilliseconds);

自己造轮子系列之OOM框架AutoMapper(记一次代码优化->ExpressionTree)_第5张图片

一百万次代码执行,Expression表达式树方式耗时0.084秒,而直接写代码赋值的方式耗时0.022秒,但是使用反射的方式却使用了3.6秒。

执行次数越多,Expression表达式树的性能便越接近直接调用代码的方式。

【总结】

通过本次基础组建的完成以及一次优化,我从中学习到了Expression表达式树的实现方法以及突出的性能优势,虽然相比反射的写法更加复杂一些。

在接下来的优化中,可能会加入对Emit的支持,到时候便是一场Emit和Expression的大战,不过我再次预测结果:应该差距不是很大~ 敬请期待...

本文的完整代码以在我的github中开源,可以直接clone下来查看验证。

https://github.com/sevenTiny/SevenTiny.Bantina

下载下来的项目有多种基础组件,直接查看该组件,当然有对其他组件的意见或建议也可以直接提出来探讨,共同学习哦~

自己造轮子系列之OOM框架AutoMapper(记一次代码优化->ExpressionTree)_第6张图片

不便于github查看的同学,我这里提供了完整的代码(是有多个对象映射的重载方法的完整版本)。

公共Attribute特性标签部分:

 1 /*********************************************************
 2  * CopyRight: 7TINY CODE BUILDER. 
 3  * Version: 5.0.0
 4  * Author: 7tiny
 5  * Address: Earth
 6  * Create: 2018-04-09 16:55:16
 7  * Modify: 2018-04-09 16:55:16
 8  * E-mail: [email protected] | [email protected] 
 9  * GitHub: https://github.com/sevenTiny 
10  * Personal web site: http://www.7tiny.com 
11  * Technical WebSit: http://www.cnblogs.com/7tiny/ 
12  * Description: 
13  * Thx , Best Regards ~
14  *********************************************************/
15 using System;
16 
17 namespace SevenTiny.Bantina.AutoMapper
18 {
19     [AttributeUsage(AttributeTargets.Property, Inherited = true)]
20     public class DoNotMapperAttribute :Attribute
21     {
22     }
23 }
View Code
 1 /*********************************************************
 2  * CopyRight: 7TINY CODE BUILDER. 
 3  * Version: 5.0.0
 4  * Author: 7tiny
 5  * Address: Earth
 6  * Create: 2018-04-03 13:28:38
 7  * Modify: 2018-04-03 13:28:38
 8  * E-mail: [email protected] | [email protected] 
 9  * GitHub: https://github.com/sevenTiny 
10  * Personal web site: http://www.7tiny.com 
11  * Technical WebSit: http://www.cnblogs.com/7tiny/ 
12  * Description: 
13  * Thx , Best Regards ~
14  *********************************************************/
15 using System;
16 using System.Linq;
17 using System.Reflection;
18 
19 namespace SevenTiny.Bantina.AutoMapper
20 {
21     [AttributeUsage(AttributeTargets.Property, Inherited = true)]
22     public class MapperAttribute : Attribute
23     {
24         public string TargetName { get; set; }
25 
26         public MapperAttribute() { }
27         public MapperAttribute(string targetName)
28         {
29             this.TargetName = targetName;
30         }
31 
32         public static string GetTargetName(PropertyInfo property)
33         {
34             var attr = property.GetCustomAttributes(true).FirstOrDefault();
35             return attr != null ? (attr as MapperAttribute).TargetName ?? default(string) : default(string);
36         }
37     }
38 }
View Code
 1 /*********************************************************
 2  * CopyRight: 7TINY CODE BUILDER. 
 3  * Version: 5.0.0
 4  * Author: 7tiny
 5  * Address: Earth
 6  * Create: 2018-04-03 13:28:38
 7  * Modify: 2018-04-03 13:28:38
 8  * E-mail: [email protected] | [email protected] 
 9  * GitHub: https://github.com/sevenTiny 
10  * Personal web site: http://www.7tiny.com 
11  * Technical WebSit: http://www.cnblogs.com/7tiny/ 
12  * Description: 
13  * Thx , Best Regards ~
14  *********************************************************/
15 using System;
16 using System.Linq;
17 
18 namespace SevenTiny.Bantina.AutoMapper
19 {
20     [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field, Inherited = true)]
21     public class MapperClassAttribute : Attribute
22     {
23         public string Name { get; set; }
24         public static string GetName(Type type)
25         {
26             var attr = type.GetCustomAttributes(typeof(MapperClassAttribute), true).FirstOrDefault();
27             return attr != null ? (attr as MapperClassAttribute).Name ?? default(string) : default(string);
28         }
29     }
30 }
View Code

一、反射版本

  1 /*********************************************************
  2  * CopyRight: 7TINY CODE BUILDER. 
  3  * Version: 5.0.0
  4  * Author: 7tiny
  5  * Address: Earth
  6  * Create: 2018-03-16 10:11:43
  7  * Modify: 2018-4-3 11:35:53
  8  * E-mail: [email protected] | [email protected] 
  9  * GitHub: https://github.com/sevenTiny 
 10  * Personal web site: http://www.7tiny.com 
 11  * Technical WebSit: http://www.cnblogs.com/7tiny/ 
 12  * Description: 
 13  * Thx , Best Regards ~
 14  *********************************************************/
 15 using System;
 16 using System.Collections.Generic;
 17 using System.Linq;
 18 using System.Linq.Expressions;
 19 using System.Reflection;
 20 
 21 namespace SevenTiny.Bantina.AutoMapper
 22 {
 23     public sealed class Mapper
 24     {
 25         private Mapper() { }
 26         /// 
 27         /// Init Source Value Dic
 28         /// 
 29         /// 
 30         /// 
 31         /// 
 32         private static Dictionary<string, object> Initdic(TSource source)
 33         {
 34             Dictionary<string, object> dic = new Dictionary<string, object>();
 35             foreach (PropertyInfo property in typeof(TSource).GetProperties())
 36             {
 37                 string targetPropertyName = MapperAttribute.GetTargetName(property);
 38                 if (!string.IsNullOrEmpty(targetPropertyName))
 39                 {
 40                     if (!dic.ContainsKey(targetPropertyName))
 41                     {
 42                         dic.Add(targetPropertyName, property.GetValue(source));
 43                     }
 44                 }
 45                 else if (!dic.ContainsKey(property.Name))
 46                 {
 47                     dic.Add(property.Name, property.GetValue(source));
 48                 }
 49             }
 50             return dic;
 51         }
 52         /// 
 53         /// SetValue from propertyinfo and sourceDictionary
 54         /// 
 55         /// 
 56         /// 
 57         /// 
 58         /// 
 59         /// 
 60         private static TValue SetValue(TValue value, PropertyInfo[] propertyInfos, Dictionary<string, object> sourceDic, Dictionary<string, string> keys) where TValue : class
 61         {
 62             foreach (PropertyInfo property in propertyInfos)
 63             {
 64                 if (!keys.ContainsKey(property.Name))
 65                 {
 66                     if (sourceDic.ContainsKey(property.Name))
 67                     {
 68                         try
 69                         {
 70                             property.SetValue(value, sourceDic[property.Name]);
 71                             keys.Add(property.Name, string.Empty);
 72                         }
 73                         catch (Exception)
 74                         {
 75                             property.SetValue(value, null);
 76                         }
 77                     }
 78                 }
 79             }
 80             return value;
 81         }
 82         /// 
 83         /// AutoMapper
 84         /// 
 85         /// value type
 86         /// source type
 87         /// 
 88         /// 
 89         public static TValue AutoMapper(TSource source) where TValue : class where TSource : class
 90         {
 91             TValue value = Activator.CreateInstance();
 92             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
 93             Dictionary<string, string> keys = new Dictionary<string, string>();
 94             value = SetValue(value, propertyInfos, Initdic(source), keys);
 95             return value;
 96         }
 97         /// 
 98         /// AutoMapper,Support for Use Action to custom special fields.
 99         /// 
100         /// 
101         /// 
102         /// 
103         /// 
104         /// 
105         public static TValue AutoMapper(TSource source, Action action) where TValue : class where TSource : class
106         {
107             TValue value = Activator.CreateInstance();
108             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
109             Dictionary<string, string> keys = new Dictionary<string, string>();
110             value = SetValue(value, propertyInfos, Initdic(source), keys);
111             action(value);
112             return value;
113         }
114         /// 
115         /// AutoMapper with multitype properties.
116         /// 
117         /// 
118         /// 
119         /// 
120         /// 
121         /// 
122         /// 
123         public static TValue AutoMapper(TSource1 source1, TSource2 source2) where TValue : class where TSource1 : class where TSource2 : class
124         {
125             TValue value = Activator.CreateInstance();
126             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
127             Dictionary<string, string> keys = new Dictionary<string, string>();
128             value = SetValue(value, propertyInfos, Initdic(source1), keys);
129             value = SetValue(value, propertyInfos, Initdic(source2), keys);
130             return value;
131         }
132         /// 
133         /// AutoMapper with multitype properties.Support for Use Action to custom special fields.
134         /// 
135         /// 
136         /// 
137         /// 
138         /// 
139         /// 
140         /// 
141         /// 
142         public static TValue AutoMapper(TSource1 source1, TSource2 source2, Action action) where TValue : class where TSource1 : class where TSource2 : class
143         {
144             TValue value = Activator.CreateInstance();
145             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
146             Dictionary<string, string> keys = new Dictionary<string, string>();
147             value = SetValue(value, propertyInfos, Initdic(source1), keys);
148             value = SetValue(value, propertyInfos, Initdic(source2), keys);
149             action(value);
150             return value;
151         }
152         /// 
153         /// AutoMapper with multitype properties.
154         /// 
155         /// 
156         /// 
157         /// 
158         /// 
159         /// 
160         /// 
161         /// 
162         /// 
163         public static TValue AutoMapper(TSource1 source1, TSource2 source2, TSource3 source3) where TValue : class where TSource1 : class where TSource2 : class where TSource3 : class
164         {
165             TValue value = Activator.CreateInstance();
166             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
167             Dictionary<string, string> keys = new Dictionary<string, string>();
168             value = SetValue(value, propertyInfos, Initdic(source1), keys);
169             value = SetValue(value, propertyInfos, Initdic(source2), keys);
170             value = SetValue(value, propertyInfos, Initdic(source3), keys);
171             return value;
172         }
173         /// 
174         /// AutoMapper with multitype properties.Support for Use Action to custom special fields.
175         /// 
176         /// 
177         /// 
178         /// 
179         /// 
180         /// 
181         /// 
182         /// 
183         /// 
184         /// 
185         public static TValue AutoMapper(TSource1 source1, TSource2 source2, TSource3 source3, Action action) where TValue : class where TSource1 : class where TSource2 : class where TSource3 : class
186         {
187             TValue value = Activator.CreateInstance();
188             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
189             Dictionary<string, string> keys = new Dictionary<string, string>();
190             value = SetValue(value, propertyInfos, Initdic(source1), keys);
191             value = SetValue(value, propertyInfos, Initdic(source2), keys);
192             value = SetValue(value, propertyInfos, Initdic(source3), keys);
193             action(value);
194             return value;
195         }
196         /// 
197         /// AutoMapper with multitype properties.
198         /// 
199         /// 
200         /// 
201         /// 
202         /// 
203         /// 
204         /// 
205         /// 
206         /// 
207         /// 
208         /// 
209         public static TValue AutoMapper(TSource1 source1, TSource2 source2, TSource3 source3, TSource4 source4) where TValue : class where TSource1 : class where TSource2 : class where TSource3 : class where TSource4 : class
210         {
211             TValue value = Activator.CreateInstance();
212             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
213             Dictionary<string, string> keys = new Dictionary<string, string>();
214             value = SetValue(value, propertyInfos, Initdic(source1), keys);
215             value = SetValue(value, propertyInfos, Initdic(source2), keys);
216             value = SetValue(value, propertyInfos, Initdic(source3), keys);
217             value = SetValue(value, propertyInfos, Initdic(source4), keys);
218             return value;
219         }
220         /// 
221         /// AutoMapper with multitype properties.Support for Use Action to custom special fields.
222         /// 
223         /// 
224         /// 
225         /// 
226         /// 
227         /// 
228         /// 
229         /// 
230         /// 
231         /// 
232         /// 
233         /// 
234         public static TValue AutoMapper(TSource1 source1, TSource2 source2, TSource3 source3, TSource4 source4, Action action) where TValue : class where TSource1 : class where TSource2 : class where TSource3 : class where TSource4 : class
235         {
236             TValue value = Activator.CreateInstance();
237             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
238             Dictionary<string, string> keys = new Dictionary<string, string>();
239             value = SetValue(value, propertyInfos, Initdic(source1), keys);
240             value = SetValue(value, propertyInfos, Initdic(source2), keys);
241             value = SetValue(value, propertyInfos, Initdic(source3), keys);
242             value = SetValue(value, propertyInfos, Initdic(source4), keys);
243             action(value);
244             return value;
245         }
246         /// 
247         /// AutoMapper with multitype properties.
248         /// 
249         /// 
250         /// 
251         /// 
252         /// 
253         /// 
254         /// 
255         /// 
256         /// 
257         /// 
258         /// 
259         /// 
260         /// 
261         public static TValue AutoMapper(TSource1 source1, TSource2 source2, TSource3 source3, TSource4 source4, TSource5 source5) where TValue : class where TSource1 : class where TSource2 : class where TSource3 : class where TSource4 : class where TSource5 : class
262         {
263             TValue value = Activator.CreateInstance();
264             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
265             Dictionary<string, string> keys = new Dictionary<string, string>();
266             value = SetValue(value, propertyInfos, Initdic(source1), keys);
267             value = SetValue(value, propertyInfos, Initdic(source2), keys);
268             value = SetValue(value, propertyInfos, Initdic(source3), keys);
269             value = SetValue(value, propertyInfos, Initdic(source4), keys);
270             value = SetValue(value, propertyInfos, Initdic(source5), keys);
271             return value;
272         }
273         /// 
274         /// AutoMapper with multitype properties.Support for Use Action to custom special fields.
275         /// 
276         /// 
277         /// 
278         /// 
279         /// 
280         /// 
281         /// 
282         /// 
283         /// 
284         /// 
285         /// 
286         /// 
287         /// 
288         /// 
289         public static TValue AutoMapper(TSource1 source1, TSource2 source2, TSource3 source3, TSource4 source4, TSource5 source5, Action action) where TValue : class where TSource1 : class where TSource2 : class where TSource3 : class where TSource4 : class where TSource5 : class
290         {
291             TValue value = Activator.CreateInstance();
292             PropertyInfo[] propertyInfos = typeof(TValue).GetProperties();
293             Dictionary<string, string> keys = new Dictionary<string, string>();
294             value = SetValue(value, propertyInfos, Initdic(source1), keys);
295             value = SetValue(value, propertyInfos, Initdic(source2), keys);
296             value = SetValue(value, propertyInfos, Initdic(source3), keys);
297             value = SetValue(value, propertyInfos, Initdic(source4), keys);
298             value = SetValue(value, propertyInfos, Initdic(source5), keys);
299             action(value);
300             return value;
301         }
302     }
303 }
View Code

二、Expression Tree 表达式树版本

  1 /*********************************************************
  2  * CopyRight: 7TINY CODE BUILDER. 
  3  * Version: 5.0.0
  4  * Author: 7tiny
  5  * Address: Earth
  6  * Create: 2018-03-16 10:11:43
  7  * Modify: 2018-4-3 11:35:53
  8  * E-mail: [email protected] | [email protected] 
  9  * GitHub: https://github.com/sevenTiny 
 10  * Personal web site: http://www.7tiny.com 
 11  * Technical WebSit: http://www.cnblogs.com/7tiny/ 
 12  * Description: 
 13  * Thx , Best Regards ~
 14  *********************************************************/
 15 using System;
 16 using System.Collections.Generic;
 17 using System.Linq;
 18 using System.Linq.Expressions;
 19 using System.Reflection;
 20 
 21 namespace SevenTiny.Bantina.AutoMapper
 22 {
 23     public sealed class Mapper where TOut : class where TIn : class
 24     {
 25         private Mapper() { }
 26         private static readonly Func funcCache = GetFunc();
 27         public static TOut AutoMapper(TIn tIn)
 28         {
 29             return funcCache(tIn);
 30         }
 31         public static TOut AutoMapper(TIn tIn, Action action)
 32         {
 33             TOut outValue = funcCache(tIn);
 34             action(outValue);
 35             return outValue;
 36         }
 37         private static Func GetFunc()
 38         {
 39             Type[] types = new Type[] { typeof(TIn) };
 40             MemberInitExpression memberInitExpression;
 41             List parameterExpressionList;
 42             MapperExpressionCommon.GetFunc(typeof(TOut), types, out memberInitExpression, out parameterExpressionList);
 43             Expression> lambda = Expression.Lambda>(memberInitExpression, parameterExpressionList);
 44             return lambda.Compile();
 45         }
 46     }
 47     public sealed class Mapper where TOut : class where TIn1 : class where TIn2 : class
 48     {
 49         private Mapper() { }
 50         public static TOut AutoMapper(TIn1 tIn1, TIn2 tIn2)
 51         {
 52             return funcCache(tIn1, tIn2);
 53         }
 54         public static TOut AutoMapper(TIn1 tIn1, TIn2 tIn2, Action action)
 55         {
 56             TOut outValue = funcCache(tIn1, tIn2);
 57             action(outValue);
 58             return outValue;
 59         }
 60         private static readonly Func funcCache = GetFunc();
 61         private static Func GetFunc()
 62         {
 63             Type[] types = new Type[] { typeof(TIn1), typeof(TIn2) };
 64             MemberInitExpression memberInitExpression;
 65             List parameterExpressionList;
 66             MapperExpressionCommon.GetFunc(typeof(TOut), types, out memberInitExpression, out parameterExpressionList);
 67             Expression> lambda = Expression.Lambda>(memberInitExpression, parameterExpressionList);
 68             return lambda.Compile();
 69         }
 70     }
 71     public sealed class Mapper where TOut : class where TIn1 : class where TIn2 : class where TIn3 : class
 72     {
 73         private Mapper() { }
 74         public static TOut AutoMapper(TIn1 tIn1, TIn2 tIn2, TIn3 tIn3)
 75         {
 76             return funcCache(tIn1, tIn2, tIn3);
 77         }
 78         public static TOut AutoMapper(TIn1 tIn1, TIn2 tIn2, TIn3 tIn3, Action action)
 79         {
 80             TOut outValue = funcCache(tIn1, tIn2, tIn3);
 81             action(outValue);
 82             return outValue;
 83         }
 84         private static readonly Func funcCache = GetFunc();
 85         private static Func GetFunc()
 86         {
 87             Type[] types = new Type[] { typeof(TIn1), typeof(TIn2), typeof(TIn3) };
 88             MemberInitExpression memberInitExpression;
 89             List parameterExpressionList;
 90             MapperExpressionCommon.GetFunc(typeof(TOut), types, out memberInitExpression, out parameterExpressionList);
 91             Expression> lambda = Expression.Lambda>(memberInitExpression, parameterExpressionList);
 92             return lambda.Compile();
 93         }
 94     }
 95     public sealed class Mapper where TOut : class where TIn1 : class where TIn2 : class where TIn3 : class where TIn4 : class
 96     {
 97         private Mapper() { }
 98         public static TOut AutoMapper(TIn1 tIn1, TIn2 tIn2, TIn3 tIn3, TIn4 tIn4)
 99         {
100             return funcCache(tIn1, tIn2, tIn3, tIn4);
101         }
102         public static TOut AutoMapper(TIn1 tIn1, TIn2 tIn2, TIn3 tIn3, TIn4 tIn4, Action action)
103         {
104             TOut outValue = funcCache(tIn1, tIn2, tIn3, tIn4);
105             action(outValue);
106             return outValue;
107         }
108         private static readonly Func funcCache = GetFunc();
109         private static Func GetFunc()
110         {
111             Type[] types = new Type[] { typeof(TIn1), typeof(TIn2), typeof(TIn3), typeof(TIn4) };
112             MemberInitExpression memberInitExpression;
113             List parameterExpressionList;
114             MapperExpressionCommon.GetFunc(typeof(TOut), types, out memberInitExpression, out parameterExpressionList);
115             Expression> lambda = Expression.Lambda>(memberInitExpression, parameterExpressionList);
116             return lambda.Compile();
117         }
118     }
119     public sealed class Mapper where TOut : class where TIn1 : class where TIn2 : class where TIn3 : class where TIn4 : class where TIn5 : class
120     {
121         private Mapper() { }
122         public static TOut AutoMapper(TIn1 tIn1, TIn2 tIn2, TIn3 tIn3, TIn4 tIn4, TIn5 tIn5)
123         {
124             return funcCache(tIn1, tIn2, tIn3, tIn4, tIn5);
125         }
126         public static TOut AutoMapper(TIn1 tIn1, TIn2 tIn2, TIn3 tIn3, TIn4 tIn4, TIn5 tIn5, Action action)
127         {
128             TOut outValue = funcCache(tIn1, tIn2, tIn3, tIn4, tIn5);
129             action(outValue);
130             return outValue;
131         }
132         private static readonly Func funcCache = GetFunc();
133         private static Func GetFunc()
134         {
135             Type[] types = new Type[] { typeof(TIn1), typeof(TIn2), typeof(TIn3), typeof(TIn4), typeof(TIn5) };
136             MemberInitExpression memberInitExpression;
137             List parameterExpressionList;
138             MapperExpressionCommon.GetFunc(typeof(TOut), types, out memberInitExpression, out parameterExpressionList);
139             Expression> lambda = Expression.Lambda>(memberInitExpression, parameterExpressionList);
140             return lambda.Compile();
141         }
142     }
143 }
View Code

你可能感兴趣的:(自己造轮子系列之OOM框架AutoMapper(记一次代码优化->ExpressionTree))