C#教程之自己動手寫映射第六節[封裝列表]

來源:互聯網
上載者:User

一、動機

  經過了前面幾節的闡述,我們已經可以通過"動態產生SQL"與"反射機制"完成簡單的對象與資料表的映射。如:Add(object obj)、Remove(object obj)等。好的,我們看如下代碼:

 1    public static List<Model.A> GetList(int PageSize, int CurrentPageIndex, out int TotalCount) 2         { 3             List<Model.A> listA = new List<Model.A>(); 4             DataSet ds = SqlCommon.GetList(conn, "ID", PageSize, PageSize * (CurrentPageIndex - 1), "A", "1=1", out TotalCount); 5             if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) 6             { 7                 foreach (DataRow dr in ds.Tables[0].Rows) 8                 { 9                     Model.A employee = new Model.A();10                     employee.ID = Convert.ToInt32(dr["ID"]);11                     employee.Name = dr["Name"].ToString();12                     employee.Password = dr["Password"].ToString();13                     employee.Department = dr["Department"].ToString();14                     employee.Position = dr["Position"].ToString();15                     listA.Add(employee);16                 }17             }18             return listA;19         }20 21    public static List<Model.B> GetList(int PageSize, int CurrentPageIndex, out int TotalCount)22         {23             List<Model.B> listB = new List<Model.B>();24             DataSet ds = SqlCommon.GetList(conn, "ID", PageSize, PageSize * (CurrentPageIndex - 1), "B", "1=1", out TotalCount);25             if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)26             {27                 foreach (DataRow dr in ds.Tables[0].Rows)28                 {29                     Model.B employee = new Model.B();30                     employee.ID = Convert.ToInt32(dr["ID"]);31                     employee.Name = dr["Name"].ToString();32                     employee.Password = dr["Password"].ToString();33                     employee.Department = dr["Department"].ToString();34                     employee.Position = dr["Position"].ToString();35                     listB.Add(employee);36                 }37             }38             return listB;39         }

  我們在擷取列表的時候總會對DataSet或DataReader裡的資料進行轉換,並放入到DTO中。 

二、構想

  我們是否可以把重複書寫的代碼進行封裝並對外提供介面以簡化我們的工作量呢...如下所示:

1         public static List<object> GetList(object classObject, string AssemblyName, string ConnString);2         public static List<object> GetList(object classObject, string strWHERE, string AssemblyName, string ConnString);3         public static List<object> GetList(object classObject, int intPageSize, int intCurrentCount, out int intTotalCount, string AssemblyName, string ConnString);4         public static List<object> GetList(object classObject, int intPageSize, int intCurrentCount, string strWhere, out int intTotalCount, string AssemblyName, string ConnString);

   這樣我們再通過類型轉換就可以使用List了.

三、實現

  我們依然是依賴對象映射去轉換DataSet,如下代碼所示:

 1        /// <summary> 2         /// 把DataSet 轉換成為List 3         /// </summary> 4         /// <param name="ds"></param> 5         /// <param name="type"></param> 6         /// <param name="BaseFieldMapping"></param> 7         /// <returns></returns> 8         internal static List<object> ChangeDateSetToList(object classObject, Dictionary<string, string> FieldMapping, DataSet ds) 9         {10             //擷取傳入的類型11             Type type = classObject.GetType();12             //建立傳回值13             List<object> tempList = new List<object>();14 15             //檢查DataSet的Table和Rows是否存在,至少取出一條資料16             if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)17             {18                 //擷取屬性名稱及類型字典19                 PropertyInfo[] properInfo = type.GetProperties();20                 Dictionary<string, Type> proDictionary = new Dictionary<string, Type>();21                 foreach (PropertyInfo proper in properInfo)22                 {23                     proDictionary.Add(proper.Name, proper.PropertyType);24                 }25 26                 //遍曆DataSet27                 int intRowCount = ds.Tables[0].Rows.Count;28                 for (int j = 0; j < intRowCount; j++)29                 {30                     //建立一個傳入類型的新的執行個體31                     object typeTempObject = type.InvokeMember(null, BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);32                     //屬性賦值33                     foreach (string strKey in FieldMapping.Keys)34                     {35                         //根據資料類型取出值36                         object[] arrObject = Model.GetProType(classObject.GetType().GetProperty(strKey).PropertyType, ds.Tables[0].Rows[j][FieldMapping[strKey]].ToString());37                         //屬性賦值38                         type.InvokeMember(strKey, BindingFlags.SetProperty, null, typeTempObject, arrObject);39                     }40                     //將實體添加到 List 41                     tempList.Add(typeTempObject);42                     //清Null 物件引用43                     typeTempObject = null;44                 }45             }46             return tempList;47         }

四、著作權

  轉載請註明出處:http://www.cnblogs.com/iamlilinfeng

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.