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

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

編輯:關於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. }
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved