程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#基礎系列:實現自己的ORM(MiniORM的測試代碼)

C#基礎系列:實現自己的ORM(MiniORM的測試代碼)

編輯:關於C#

看到有同學反饋,說MiniORM代碼有Bug,也不知道怎麼用,所以今天我就寫了些測試代碼。希望能夠給點幫助。

同時也發現了兩Bug,一並羅列出:

1、MiniORM.PubFuncs.GetObjectType()函數:

1.public static Type GetObjectType(string assemblyname, string namespacename, string classname)
2.{
3. Type objType = (Type)_HashObjectType[assemblyname + namespacename + classname];
4.
5. if (objType == null)
6. {
7. object obj = _HashObjectType[assemblyname + namespacename + classname];
8.
9. obj = Assembly.Load(assemblyname).CreateInstance(namespacename + "." + classname);
10.
11. _HashObjectType[assemblyname + namespacename + classname] = obj.GetType();
12.
13. //需要增加這句
14. objType = (Type)_HashObjectType[assemblyname + namespacename + classname];
15. }
16.
17. return objType;
18.}

2、MiniORM.OrmWriter.CheckValidate()函數:

1.public void CheckValidate(object ModelObject)
2. {
3. PropertyInfo[] infos = ModelObject.GetType().GetProperties();
4. object[] CustomerAttributes = null;
5. object[] CustomerAttributes1 = null;
6.
7. //SELECT ID FROM TableName WHERE @A='' OR @B=''
8. string strSQL = "SELECT ID FROM {0} WHERE {1}";
9. string strTablename = PubFuncs.GetTableName(ModelObject);
10. string strWOA = "";
11. string strWHERE = "";
12. string strFieldMessage = "";
13.
14. foreach (PropertyInfo info in infos)
15. {
16. CustomerAttributes = info.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldNotDoubleAttribute), false);
17. CustomerAttributes1 = info.GetCustomAttributes(typeof(MiniORMAttribute.DataFieldAttribute), false);
18.
19. if (CustomerAttributes != null && CustomerAttributes1 != null)
20. {
21. for (int i = 0; i < CustomerAttributes.Length; i++)
22. {
23. strWHERE += strWOA + ((MiniORMAttribute.DataFieldAttribute)CustomerAttributes1[0]).FieldName + "='" + info.GetValue(ModelObject, null) + "'";
24. strFieldMessage += ((MiniORMAttribute.DataFieldAttribute)CustomerAttributes1[0]).FieldName;
25. strWOA = " OR ";
26. }
27. }
28. }
29.
30. //這裡需要做strWHERE不為空的判斷
31. if (strWHERE.Trim() != "")
32. {
33. strSQL = string.Format(strSQL, new string[] { strTablename, strWHERE });
34.
35. using (SqlConnection conn = new SqlConnection(PubFuncs.ConnectionStr))
36. {
37. conn.Open();
38. SqlCommand cmd = new SqlCommand();
39. cmd.Connection = conn;
40. cmd.CommandType = CommandType.Text;
41. cmd.CommandText = strSQL;
42.
43. using (SqlDataReader rdr = cmd.ExecuteReader())
44. {
45. if (rdr.Read())
46. {
47. throw new Exception("下列字段值不能重復:" + strFieldMessage);
48. }
49. }
50. }
51. }
52. }

測試代碼如下:

首先是類關系圖:

數據庫表:

Person表:

Hand表:

Head表:

測試代碼如下:

下面是類代碼:

1.[DataObjectAttribute("Person", "ID", "")]
2.public class Person
3.{
4. private int _ID;
5. private string _Name;
6. private int _Age;
7. private string _Sex;
8. private Head _Head;
9. private Hand _LeftHand;
10. private Hand _RightHand;
11.
12. [DataFieldAttribute("ID", true)]
13. public int ID
14. {
15. get { return _ID; }
16. set { _ID = value; }
17. }
18.
19. [SubDataObjectAttribute(SubDataObjectFieldType.Object, "MiniORM", "MiniORM", "Head")]
20. public Head Head
21. {
22. get { return _Head; }
23. set { _Head = value; }
24. }
25.
26. [SubDataObjectAttribute(SubDataObjectFieldType.Object, "MiniORM", "MiniORM", "Hand")]
27. public Hand LeftHand
28. {
29. get { return _LeftHand; }
30. set { _LeftHand = value; }
31. }
32.
33. [SubDataObjectAttribute(SubDataObjectFieldType.Object, "MiniORM", "MiniORM", "Hand")]
34. public Hand RightHand
35. {
36. get { return _RightHand; }
37. set { _RightHand = value; }
38. }
39.
40. [DataFieldAttribute("name", "NvarChar")]
41. public string Name
42. {
43. get { return this._Name; }
44. set { this._Name = value; }
45. }
46.
47. [DataFieldAttribute("age", "int")]
48. public int Age
49. {
50. get { return this._Age; }
51. set { this._Age = value; }
52. }
53.
54. [DataFieldAttribute("sex", "NvarChar")]
55. public string Sex
56. {
57. get { return this._Sex; }
58. set { this._Sex = value; }
59. }
60.
61. public Hand Hand
62. {
63. get
64. {
65. throw new System.NotImplementedException();
66. }
67. set
68. {
69. }
70. }
71.
72. public Head Head1
73. {
74. get
75. {
76. throw new System.NotImplementedException();
77. }
78. set
79. {
80. }
81. }
82.}
83.
84.[DataObjectAttribute("Hand", "ID", "PersonID")]
85.public class Hand
86.{
87. private int _ID;
88. private int _PersonID;
89. private string _Name;
90.
91. [DataFieldAttribute("ID", true)]
92. public int ID
93. {
94. get { return this._ID; }
95. set { this._ID = value; }
96. }
97.
98. [DataFieldAttribute("PersonID")]
99. public int PersonID
100. {
101. get { return this._PersonID; }
102. set { this._PersonID = value; }
103. }
104.
105. [DataFieldAttribute("name", "NvarChar")]
106. public string Name
107. {
108. get { return _Name; }
109. set { _Name = value; }
110. }
111.}
112.
113.[DataObjectAttribute("Head", "ID", "PersonID")]
114.public class Head
115.{
116. private int _ID;
117. private int _PersonID;
118. private string _Name;
119.
120. [DataFieldAttribute("ID", true)]
121. public int ID
122. {
123. get { return this._ID; }
124. set { this._ID = value; }
125. }
126.
127. [DataFieldAttribute("PersonID")]
128. public int PersonID
129. {
130. get { return this._PersonID; }
131. set { this._PersonID = value; }
132. }
133.
134. [DataFieldAttribute("name", "NvarChar")]
135. public string Name
136. {
137. get { return _Name; }
138. set { _Name = value; }
139. }
140.}

下面是測試代碼:

1.[STAThread]
2.static void Main()
3.{
4. //插入測試
5. WriteTest();
6.
7. //讀取測試
8. ReadTest();
9.}
10.
11.private static void WriteTest()
12.{
13. Person person = new Person();
14. person.Name = "snoopy";
15. person.Age = 5;
16. person.Sex = "male";
17.
18. Hand lefthand = new Hand();
19. lefthand.Name = "左手";
20.
21. Hand righthand = new Hand();
22. righthand.Name = "右手";
23.
24. Head head = new Head();
25. head.Name = "大笨腦袋";
26.
27. person.LeftHand = lefthand;
28. person.RightHand = righthand;
29. person.Head = head;
30.
31. MiniORM.OrmWriter write = new OrmWriter();
32. write.Write(person);
33.}
34.
35.private static void ReadTest()
36.{
37. MiniORM.OrmReader reader = new OrmReader();
38. Person person = (Person)reader.Read(new Person(), 1);
39.
40.
41. Console.WriteLine("person.Name:" + person.Name);
42. Console.WriteLine("person.Age:" + person.Age);
43. Console.WriteLine("person.Sex:" + person.Sex);
44. Console.WriteLine("LeftHand.Name:" + person.LeftHand.Name);
45. Console.WriteLine("RightHand.Name:" + person.RightHand.Name);
46. Console.WriteLine("Head.Name:" + person.Head.Name);
47.
48. Console.Read();
49.}

ReadTest的測試結果如圖:

好了,各位,你可以使用這個調試代碼來試試了。。。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved