程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C#發現之旅第十二講 基於反射和動態編譯的快速ORM框架(上)(4)

C#發現之旅第十二講 基於反射和動態編譯的快速ORM框架(上)(4)

編輯:關於C語言

基礎 類型RecordORMHelper

本類型屬於ORM框架的底層模塊。其代碼為

public abstract class RecordORMHelper
{
     ///  <summary>
     /// 對象操作的數據表名稱
     ///  </summary>
     public abstract string TableName
      {
         get ;
     }

     ///  <summary>
     /// 從數據讀取器讀取數據創建一個記錄對象
      /// </summary>
     /// <param name="reader">數 據讀取器</param>
     /// <returns>讀取的數據 </returns>
     public object ReadRecord(  System.Data.IDataReader reader )
     {
         int[]  indexs = GetFIEldIndexs( reader );
         return  InnerReadRecord( reader  ,indexs );
     }

     ///  <summary>
     /// 從數據讀取器讀取數據創建若干個記錄對象
     /// </summary>
     /// <param  name="reader">數據讀取器</param>
     /// <param  name="MaxRecordCount">允許讀取的最大的記錄個數,為0則無限制</param>
     /// <returns>讀取的數據對象列表</returns>
      public System.Collections.ArrayList ReadRecords( System.Data.IDataReader  reader , int MaxRecordCount )
     {
          System.Collections.ArrayList list = new System.Collections.ArrayList();
         int[] indexs = GetFIEldIndexs( reader );
          while( reader.Read())
         {
               object record = InnerReadRecord( reader , indexs );
               list.Add( record );
              if(  MaxRecordCount > 0 && list.Count >= MaxRecordCount )
               {
                   break;
              }
         }//while
          return list ;
     }

     ///  <summary>
     /// 從一個數據讀取器中讀取一條記錄對象,必須重載
     /// </summary>
     /// <param  name="reader">數據讀取器</param>
     /// <param  name="FIEldIndexs">字段序號列表</param>
     ///  <returns>讀取的記錄對象</returns>
     protected abstract  object InnerReadRecord( System.Data.IDataReader reader , int[]  FIEldIndexs );

 
     /// <summary>
      /// 為刪除記錄而初始化數據庫命令對象
     /// </summary>
     /// <param name="cmd">數據庫命令對象</param>
      /// <param name="objRecord">實體對象實例</param>
      /// <returns>添加的SQL參數個數</returns>
     public  abstract int FillDeleteCommand( System.Data.IDbCommand cmd , object  objRecord );

     /// <summary>
     /// 為插 入記錄而初始化數據庫命令對象
     /// </summary>
      /// <param name="cmd">數據庫命令對象</param>
     ///  <param name="objRecord">實體對象實例</param>
     ///  <returns>添加的SQL參數個數</returns>
     public abstract  int FillInsertCommand( System.Data.IDbCommand cmd , object objRecord  );

     /// <summary>
     /// 為更新數據庫記 錄而初始化數據庫命令對象
     /// </summary>
     ///  <param name="cmd">數據庫命令對象</param>
     ///  <param name="objRecord">實體對象實例</param>
     ///  <returns>添加的SQL參數個數</returns>
     public abstract  int FillUpdateCommand( System.Data.IDbCommand cmd , object objRecord  );

     /// <summary>
     /// 字段列表數組
     /// </summary>
     protected abstract string [] RecordFIEldNames
     {
         get ;
      }

     /// <summary>
     /// 針對特定的數據 讀取器獲得實體對象的各個屬性對應的數據欄目的編號
     ///  </summary>
     /// <param name="reader">數據讀取器 </param>
     /// <returns>編號列表</returns>
     private int[] GetFIEldIndexs( System.Data.IDataReader reader )
     {
         if( reader == null )
          {
              throw new ArgumentNullException ("reader");
         }

         string[]  FieldNames = this.RecordFIEldNames ;

         int[]  indexs = new int[ FIEldNames.Length ] ;
         int  FieldCount = reader.FIEldCount ;
         string[] names =  new string[ FIEldCount ] ;
         for( int iCount = 0 ;  iCount < FIEldCount ; iCount ++ )
         {
               names[ iCount ] = reader.GetName( iCount ) ;
          }
         for( int iCount = 0 ; iCount <  indexs.Length ; iCount ++ )
         {
               indexs[ iCount ] = -1 ;
              string  name = FIEldNames[ iCount ] ;
              for( int  iCount2 = 0 ; iCount2 < FIEldCount ; iCount2 ++ )
               {
                   if(  EqualsFIEldName( name , names[ iCount2 ] ))
                    {
                       indexs[ iCount  ] = iCount2 ;
                       break;
                   }
              }
         }
         for( int iCount = 0 ; iCount  < FIEldCount ; iCount ++ )
         {
               string name = reader.GetName( iCount );
               for( int iCount2 = 0 ; iCount2 < indexs.Length ; iCount2  ++ )
              {
                    if( EqualsFieldName( name , FIEldNames[ iCount2 ] ))
                    {
                        indexs[ iCount2 ] = iCount ;
                   }
              }
         }

          return indexs ;
     }

     ///  <summary>
     /// 連接多個字符串,各個字符串之間用逗號分隔,本函 數會在動態生成的派生類中使用
     /// </summary>
      /// <param name="strs">字符串集合</param>
     ///  <returns>連接所得的大字符串</returns>
     protected  string ConcatStrings( System.Collections.IEnumerable strs )
      {
         System.Text.StringBuilder myStr = new  System.Text.StringBuilder();
         foreach( string str in  strs )
         {
              if(  myStr.Length > 0 )
              {
                    myStr.Append(",");
              }
              myStr.Append( str );
          }//foreach
         return myStr.ToString();
     }
     /// <summary>
     /// 判斷兩個字段名是否等價
     /// </summary>
     /// <param name="name1">字 段名1</param>
     /// <param name="name2">字段名 2</param>
     /// <returns>true:兩個字段名等價 false:字 段名不相同</returns>
     private bool EqualsFIEldName( string  name1 , string name2 )
     {
         if( name1  == null || name2 == null )
         {
               throw new ArgumentNullException("name1 or name2");
          }
         name1 = name1.Trim();
          name2 = name2.Trim();
         // 進行不區分大小寫的比較
          if( string.Compare( name1 , name2 , true ) == 0 )
         {
              return true ;
          }
         int index = name1.IndexOf(".");
          if( index > 0 )
         {
               name1 = name1.Substring( index + 1 ).Trim();
          }
         index = name2.IndexOf(".");
          if( index > 0 )
         {
               name2 = name2.Substring( index + 1 ).Trim();
          }
         return string.Compare( name1 , name2 , true )  == 0 ;
     }

     #region 從數據庫讀取的原始數據轉 換為指定數據類型的函數群,本函數會在動態生成的派生類中使用

      protected byte ConvertToByte( object v , byte DefaultValue )
      {
         if( v == null || DBNull.Value.Equals( v ))
              return DefaultValue ;
          else
              return Convert.ToByte( v );
      }

     protected sbyte ConvertToSByte( object v , sbyte  DefaultValue )
     {
         if( v == null ||  DBNull.Value.Equals( v ))
              return DefaultValue  ;
         else
              return  Convert.ToSByte( v );
     }

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