C#:DataTable映射成Model

这是数据库开发中经常遇到的问题,当然,这可以用现成的ORM框架来解决,但有些时候,如果DataSet/DataTable是第三方接口返回的,ORM就不方便了,还得自己处理。

反射自然必不可少的,另外考虑到DataTable中的ColumnName通常与Model的PropertyName并不严格对应,可以用Attribute来记录这种映射关系。

步骤1:先创建一个DataFieldAttribute类

 1 using System;
 2 
 3 namespace Jimmy.ORM
 4 {
 5     [AttributeUsage(AttributeTargets.Property)]
 6     public sealed class DataFieldAttribute:Attribute
 7     {
 8         /// <summary>
 9         /// 表对应的字段名
10         /// </summary>
11         public string ColumnName { set; get; }
12 
13         public DataFieldAttribute(string columnName)
14         {
15             ColumnName = columnName;
16         }
17     }
18 }
View Code

 

步骤2:在Model/Entity的Class成员上,应用DataField特性,参见下面的代码:

 1 using System;
 2 
 3 namespace Jimmy.ORM.Entity
 4 {
 5     [Serializable]
 6     public class ProductEntity : DataEntityBase
 7     {
 8 
 9         [DataField("PRODUCT_NO")]
10         public string ProductNo { set; get; }
11 
12         [DataField("PRODUCT_ID")]
13         public int ProductId { set; get; }
14 
15         [DataField("PRODUCT_NAME")]
16         public string ProductName { set; get; }
17 
18         public override string ToString()
19         {
20             return string.Format("ProductNo:{1}{0}ProductId:{2}{0}ProductName:{3}", Environment.NewLine, ProductNo,
21                                  ProductId, ProductName);
22         }
23     }
24 }
View Code

 

步骤3:该反射出场了,为了方便起见,封装了一个DataConvert类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Data;
 4 using System.Reflection;
 5 
 6 namespace Jimmy.ORM
 7 {
 8     /// <summary>
 9     /// 将DataRow/DataTable转换成Entity/Entity列表
10     /// </summary>
11     public static class DataConvert<T> where T : DataEntityBase, new()
12     {
13         /// <summary>
14         /// 将DataRow行转换成Entity
15         /// </summary>
16         /// <param name="dr"></param>
17         /// <returns></returns>
18         public static T ToEntity(DataRow dr)
19         {
20             T entity = new T();
21             Type info = typeof(T);
22             var members = info.GetMembers();
23             foreach (var mi in members)
24             {
25                 if (mi.MemberType == MemberTypes.Property)
26                 {
27                     //读取属性上的DataField特性
28                     object[] attributes = mi.GetCustomAttributes(typeof(DataFieldAttribute), true);
29                     foreach (var attr in attributes)
30                     {
31                         var dataFieldAttr = attr as DataFieldAttribute;
32                         if (dataFieldAttr != null)
33                         {
34                             var propInfo = info.GetProperty(mi.Name);
35                             if (dr.Table.Columns.Contains(dataFieldAttr.ColumnName))
36                             {
37                                 //根据ColumnName,将dr中的相对字段赋值给Entity属性
38                                 propInfo.SetValue(entity,
39                                                   Convert.ChangeType(dr[dataFieldAttr.ColumnName], propInfo.PropertyType),
40                                                   null);
41                             }
42 
43                         }
44                     }
45                 }
46             }
47             return entity;
48         }
49 
50         /// <summary>
51         /// 将DataTable转换成Entity列表
52         /// </summary>
53         /// <param name="dt"></param>
54         /// <returns></returns>
55         public static List<T> ToList(DataTable dt)
56         {
57             List<T> list = new List<T>(dt.Rows.Count);
58             foreach (DataRow dr in dt.Rows)
59             {
60                 list.Add(ToEntity(dr));
61             }
62             return list;
63         }
64     }
65 }
View Code

 

步骤4:测试

 1 using System;
 2 using System.Data;
 3 using Jimmy.ORM.Entity;
 4 
 5 namespace Jimmy.ORM.Test
 6 {
 7     class Program
 8     {
 9         static void Main()
10         {
11             DataTable dt = new DataTable();
12             dt.Columns.Add("PRODUCT_NO");
13             dt.Columns.Add("PRODUCT_ID");
14             dt.Columns.Add("PRODUCT_NAME");
15 
16             dt.Rows.Add("00001", 1, "手机");
17             dt.Rows.Add("00002", 2, "服装");
18 
19             var products = DataConvert<ProductEntity>.ToList(dt);
20 
21             foreach (var entity in products)
22             {
23                 Console.WriteLine(entity);
24             }
25 
26             Console.Read();
27         }
28     }
29 }
View Code

 

示例代码下载:https://files.cnblogs.com/yjmyzz/DataConvertor.zip

posted @ 2013-10-15 17:00  菩提树下的杨过  阅读(8399)  评论(0编辑  收藏  举报