一、動機
經過了前面幾節的闡述,我們已經可以通過"動態產生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