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

來源:互聯網
上載者:User

看到有同學反饋,說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. }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.