C#基础系列:实现自己的ORM(MiniORM的测试代码)

看到有同学反馈,说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.     if (objType == null)
  5.     {
  6.         object obj = _HashObjectType[assemblyname + namespacename + classname];
  7.         obj = Assembly.Load(assemblyname).CreateInstance(namespacename + "." + classname);
  8.         _HashObjectType[assemblyname + namespacename + classname] = obj.GetType();
  9.         //需要增加这句
  10.         objType = (Type)_HashObjectType[assemblyname + namespacename + classname];
  11.     }
  12.     return objType;
  13. }

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

测试代码如下:
首先是类关系图:

 

数据库表:

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

下面是测试代码:

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

ReadTest的测试结果如:

好了,各位,你可以使用这个调试代码来试试了。。。

 

相关文章:

C#基础系列:实现自己的ORM(ORM的基础概念)

C#基础系列:实现自己的ORM(反射以及Attribute在ORM中的应用)

C#基础系列:实现自己的ORM(构造我自己的ORM)

 

源代码下载

 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页