zoukankan      html  css  js  c++  java
  • 菜鸟类库诞生记二:通过反射转换DataRow为对象

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

    所以在数据量不是很大的情况下,推荐使用。

    如果数据量很大,可以使用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 }

    好了,就这么多了。

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

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

    以同步至:个人文章目录索引

  • 相关阅读:
    Windows10 下Apache服务器搭建
    Visual Studio 2019及其注册码
    清理300多台MySQL数据库的过期binlog日志
    JS获取和设置光标的位置
    在android客户端加载html源代码总结
    OpenGL ES2学习笔记(9)-- 转换矩阵
    可行性研究报告 之机房收费系统的可行性研究报告
    eclipse 配置Maven问题解决办法:新建maven工程时报错:Could not resolve archetype org.apache.maven.archetypes .
    常见的证书格式和相互转换
    九度OJ 题目1384:二维数组中的查找
  • 原文地址:https://www.cnblogs.com/yangcaogui/p/3495923.html
Copyright © 2011-2022 走看看