简单的ORM
简单的ORM例子。
自定义属性:
实体类:
简单的ORM Helper
自定义属性
1using System;
2using System.ComponentModel;
3
4namespace Relaction.ORM
5{
6 /**//// <summary>
7 ///
8 /// </summary>
9 ///
10 [AttributeUsage(AttributeTargets.Property,Inherited = false,AllowMultiple = false)]
11 public class ColumnAttribute:Attribute
12 {
13 private string _columnName = null;
14 public string ColumnName
15 {
16 get
17 {
18 return _columnName;
19 }
20 }
21
22 public ColumnAttribute(string columnName)
23 {
24 _columnName = columnName;
25 }
26 }
27}
28
1using System;
2using System.ComponentModel;
3
4namespace Relaction.ORM
5{
6 /**//// <summary>
7 ///
8 /// </summary>
9 ///
10 [AttributeUsage(AttributeTargets.Property,Inherited = false,AllowMultiple = false)]
11 public class ColumnAttribute:Attribute
12 {
13 private string _columnName = null;
14 public string ColumnName
15 {
16 get
17 {
18 return _columnName;
19 }
20 }
21
22 public ColumnAttribute(string columnName)
23 {
24 _columnName = columnName;
25 }
26 }
27}
28
实体类:
实体类,并添加上相应属性
1using System;
2
3namespace Relaction.ORM
4{
5 /**//// <summary>
6 ///
7 /// </summary>
8 public class Customer
9 {
10 private string _customerID,_companyName,_contactName;
11 [Column("CustomerID")]
12 public string CustomerID
13 {
14 get
15 {
16 return _customerID;
17 }
18 set
19 {
20 _customerID = value;
21 }
22
23 }
24 [Column("CompanyName")]
25 public string CompanyName
26 {
27 get
28 {
29 return _companyName;
30 }
31 set
32 {
33 _companyName = value;
34 }
35 }
36 [Column("ContactName")]
37 public string ContactName
38 {
39 get
40 {
41 return _contactName;
42 }
43 set
44 {
45 _contactName = value;
46 }
47 }
48 public Customer()
49 {
50 }
51 }
52}
53
1using System;
2
3namespace Relaction.ORM
4{
5 /**//// <summary>
6 ///
7 /// </summary>
8 public class Customer
9 {
10 private string _customerID,_companyName,_contactName;
11 [Column("CustomerID")]
12 public string CustomerID
13 {
14 get
15 {
16 return _customerID;
17 }
18 set
19 {
20 _customerID = value;
21 }
22
23 }
24 [Column("CompanyName")]
25 public string CompanyName
26 {
27 get
28 {
29 return _companyName;
30 }
31 set
32 {
33 _companyName = value;
34 }
35 }
36 [Column("ContactName")]
37 public string ContactName
38 {
39 get
40 {
41 return _contactName;
42 }
43 set
44 {
45 _contactName = value;
46 }
47 }
48 public Customer()
49 {
50 }
51 }
52}
53
简单的ORM Helper
ORM Helper
1using System;
2
3using System.Collections;
4using System.ComponentModel;
5using System.Reflection;
6using System.Data;
7using System.Data.Common;
8
9namespace Relaction.ORM
10{
11 /**//// <summary>
12 ///
13 /// </summary>
14 public class FillHelper
15 {
16 public static IList Fill(Type rowType,IDataReader reader)
17 {
18 ArrayList dataList = new ArrayList();
19 while(reader.Read())
20 {
21 object item = Activator.CreateInstance(rowType,false);
22 foreach(MemberInfo mi in rowType.GetMembers())
23 {
24 foreach(ColumnAttribute attr in mi.GetCustomAttributes(typeof(ColumnAttribute),false))
25 {
26 int index = reader.GetOrdinal(attr.ColumnName);
27 if(index != -1 )
28 {
29 if(mi.MemberType == MemberTypes.Field)
30 ((FieldInfo)mi).SetValue(item,reader.GetValue(index));
31 else
32 if(mi.MemberType == MemberTypes.Property)
33 ((PropertyInfo)mi).SetValue(item,reader.GetValue(index),null);
34 }
35 }
36 }
37 dataList.Add(item);
38 }
39 return dataList;
40 }
41 public FillHelper()
42 {
43 }
44 }
45}
46
1using System;
2
3using System.Collections;
4using System.ComponentModel;
5using System.Reflection;
6using System.Data;
7using System.Data.Common;
8
9namespace Relaction.ORM
10{
11 /**//// <summary>
12 ///
13 /// </summary>
14 public class FillHelper
15 {
16 public static IList Fill(Type rowType,IDataReader reader)
17 {
18 ArrayList dataList = new ArrayList();
19 while(reader.Read())
20 {
21 object item = Activator.CreateInstance(rowType,false);
22 foreach(MemberInfo mi in rowType.GetMembers())
23 {
24 foreach(ColumnAttribute attr in mi.GetCustomAttributes(typeof(ColumnAttribute),false))
25 {
26 int index = reader.GetOrdinal(attr.ColumnName);
27 if(index != -1 )
28 {
29 if(mi.MemberType == MemberTypes.Field)
30 ((FieldInfo)mi).SetValue(item,reader.GetValue(index));
31 else
32 if(mi.MemberType == MemberTypes.Property)
33 ((PropertyInfo)mi).SetValue(item,reader.GetValue(index),null);
34 }
35 }
36 }
37 dataList.Add(item);
38 }
39 return dataList;
40 }
41 public FillHelper()
42 {
43 }
44 }
45}
46