菜鸟类库诞生记二:经过反射转换DataRow为对象

虽然大数据量的环境下,经过反射转换DataRow为对象性能会很低,可是在数据量适中的时候,这样可以减小不少的代码量,性能也确实不错。html

因此在数据量不是很大的状况下,推荐使用。post

若是数据量很大,能够使用Emit来提升性能,最近也在研究它,网上也有不少这方面的资料。性能

我定义了一个DataRow的扩张方法,以下:大数据

 1 using System;
 2 using System.Data;
 3 using System.Reflection;
 4 
 5 namespace YCG.FCL.Common.ExtensionMethods
 6 {
 7     public static class DataRowExtension
 8     {
 9         /// <summary>
10         /// Convert data row to corresponding model..
11         /// </summary>
12         /// <typeparam name="T">Model</typeparam>
13         /// <param name="dataRow">Data Row.</param>
14         /// <returns>Model Object.</returns>
15         public static T GenerateInfo<T>(this DataRow dataRow) where T : class ,new()
16         {
17             if (dataRow == null) throw new ArgumentNullException();
18             T t = new T();
19             Type type = typeof(T);
20             PropertyInfo[] propertyInfos = type.GetProperties();
21             foreach (PropertyInfo propertyInfo in propertyInfos)
22             {
23                 string propertyName = propertyInfo.Name;
24                 if (dataRow.Table.Columns.Contains(propertyName))
25                 {
26                     object value = dataRow[propertyName];
27                     if (value != null && value != DBNull.Value)
28                     {
29                         if (propertyInfo.PropertyType.IsEnum)
30                         {
31                             propertyInfo.SetValue(t, Enum.Parse(propertyInfo.PropertyType, value.ToString()), null);
32                             //propertyInfo.SetValue(t, Enum.ToObject(propertyInfo.PropertyType, value.ToInt32()), null);
33                         }
34                         else
35                         {
36                             switch (propertyInfo.PropertyType.Name)
37                             {
38                                 case "Int32":
39                                     propertyInfo.SetValue(t, value.ToInt32(), null);
40                                     break;
41                                 case "DateTime":
42                                     propertyInfo.SetValue(t, value.ToDateTime(), null);
43                                     break;
44                                 case "Boolean":
45                                     propertyInfo.SetValue(t, value.ToBool(), null);
46                                     break;
47                                 case "Double":
48                                     propertyInfo.SetValue(t, value.ToDouble(), null);
49                                     break;
50                                 //case "Byte[]":
51                                 //    propertyInfo.SetValue(t, value.ToBytes(), null);
52                                 //    break;
53                                 default:
54                                     propertyInfo.SetValue(t, value, null);
55                                     break;
56                             }
57                         }
58                     }
59                 }
60             }
61             return t;
62         }
63 
64         public static T GenerateInfo<T>(this DataRow dataRow, Func<DataRow, T> func) where T : class,new()
65         {
66             if (dataRow == null) return new T();
67             return func(dataRow);
68         }
69     }
70 }

 

好了,就这么多了。ui

最近在设计数据访问层,真的只有当本身动手去作的时候,才知道本身知识的局限性,可能要到过年以前才能完整的设计好。this

因此关于这方面的文章还要过段时间才能写出来。url

 

以同步至:我的文章目录索引spa

相关文章
相关标签/搜索