2008-09-03 C#3.0 修正 DAL BLL 使用泛型 2880 0 .NET 2008-09-21 摘要:C#3.0 修正 DAL BLL 使用泛型 繼續修改中 ...以下是近期修改過後的結果,使用兩個資料表做為範例 (News公告,Activity活動)基底類別 GenericBase /// <!--基底類別,商業邏輯元件--> /// <summary> /// 基底類別,商業邏輯元件 - design By Phoenix 2008 - /// </summary> /// <typeparam name="T">資料物件模型</typeparam> public abstract class GenericBase<T>:IInsert<T>,IDelete<T>,IUpdate<T> { /// <summary> /// 資料表名稱 /// </summary> protected DbName DataName; /// <summary> /// 資料存取元件 /// </summary> protected MySqlServer objData; /// <!--建立資料物件--> /// <summary> /// 建立資料物件 - design By Phoenix 2008 - /// </summary> /// <param name="row">DataRow(資料行)</param> /// <returns>資料物件</returns> protected abstract T CreateObject(DataRow row); /// <!--建立Insert強型別物件清單--> /// <summary> /// 建立Insert強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="objT">泛型物件</param> /// <returns>DataField強型別物件清單</returns> protected abstract List<DataField> CreateInsertList(T objT); /// <!--建立Delete強型別物件清單--> /// <summary> /// 建立Delete強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="objT">泛型物件</param> /// <returns>DataField強型別物件清單</returns> protected abstract List<DataField> CreateDeleteList(T objT); /// <!--建立Update強型別物件清單--> /// <summary> /// 建立Update強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="objT">泛型物件</param> /// <returns>DataField強型別物件清單</returns> protected abstract List<DataField> CreateUpdateList(T objT); ///<!--由DataTable(資料表)轉換成List(強型別物件清單)--> /// <summary> /// 由DataTable(資料表)轉換成List(強型別物件清單) - design By Phoenix 2008 - /// </summary> /// <param name="dt">要轉換的DataTable(資料表)</param> /// <returns>資料強型別物件清單</returns> protected List<T> ToList(DataTable dt) { //例外處理 try { //建立Board強型別物件清單 List<T> result = new List<T>(); result.Clear(); //將DataTable一一取出,呼叫CreateObject後加入清單。 foreach (DataRow row in dt.Rows) result.Add(CreateObject(row)); //回傳結果 return result; } catch { //擲回例外 throw new InvalidOperationException("ToList區段執行失敗"); } } ///<!--取得所有資料強型別物件清單--> /// <summary> /// 取得所有資料強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <returns>資料強型別物件清單</returns> public List<T> GetAll() { //例外處理 try { //field(Select欄位集合),*為取得所有欄位 string[] field = new string[] { "*" }; //執行Select介面,傳入Select欄位集合、資料表名稱,傳回資料表 DataTable dt = objData.GetTable(field, DataName); //將資料表轉換成強型別物件清單再回傳 return ToList(dt); } catch (Exception) { //擲出例外 throw; } } ///<!--取得資料強型別物件清單--> /// <summary> /// 取得資料強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="Condition">條件限制清單</param> /// <returns>資料強型別物件清單</returns> public List<T> GetBy(List<DataField> Condition) { //例外處理 try { //field(Select欄位集合),*為取得所有欄位 string[] field = new string[] { "*" }; //執行Select介面,傳入Select欄位集合、資料表名稱、條件限制清單,傳回資料表 DataTable dt = objData.GetTable(field, DataName, Condition); //將資料表轉換成強型別物件清單再回傳 return ToList(dt); } catch (Exception) { //擲出例外 throw; } } ///<!--新增一筆資料行--> /// <summary> /// 新增一筆資料行 - design By Phoenix 2008 - /// </summary> /// <param name="objT">資料物件</param> /// <returns>受影響的行數</returns> public int Insert(T objT) { //例外處理 try { //執行CreateInsertList(傳入資料清單)(傳回Insert清單) //執行Insert介面(傳入資料表名稱、Insert清單)(傳回影響的行數) //傳回受影響的行數 return objData.AddNew(DataName, CreateInsertList(objT)); } catch (Exception) { //擲出例外 throw; } } ///<!--執行刪除的動作,刪除Del標記為true的項目--> /// <summary> /// 執行刪除的動作,刪除Del標記為true的項目 - design By Phoenix 2008 - /// </summary> /// <returns>受影響的行數</returns> public int Delete() { //例外處理 try { //自訂SqlQuery查詢指令 string SqlQuery = string.Format("DELETE FROM {0} WHERE (Del = 1)", DataName.ToString()); //執行Query,傳回受影響的行數 return objData.RunQuery(SqlQuery); } catch (Exception) { //擲出例外 throw; } } ///<!--設定資料刪除標記--> /// <summary> /// 設定資料刪除標記 - design By Phoenix 2008 - /// </summary> /// <param name="objT">資料物件</param> /// <returns>受影響的行數</returns> public int Delete(T objT) { //例外處理 try { //呼叫CreateDeleteList(傳入Activity物件)(傳回Delete清單) //執行Update介面(傳入資料表名稱、Delete清單)(傳回受影響的行數) //傳回受影響的行數 return objData.Update(DataName , CreateDeleteList(objT)); } catch (Exception) { //擲出例外 throw; } } ///<!--修改資料行--> /// <summary> /// 修改資料行 - design By Phoenix 2008 - /// </summary> /// <param name="objT">資料物件</param> /// <returns>受影響的行數</returns> public int Update(T objT) { //例外處理 try { //若不為null //呼叫CreateUpdateList(傳入資料物件)(傳回Update清單) //執行Update介面(傳入資料表名稱、Update清單)(傳回影響的行數) //傳回受影響的行數 return objData.Update(DataName, CreateUpdateList(objT)); } catch (Exception) { //擲出例外 throw; } } /// <!--初始化 BusinessLogicLayer.GenericBase 類別的新執行個體--> /// <summary> /// 初始化 BusinessLogicLayer.GenericBase 類別的新執行個體 - design By Phoenix 2008 - /// </summary> /// <param name="Name">列舉-資料表名稱</param> public GenericBase(DbName Name) { DataName = Name; } #region IInsert<T> 成員 int IInsert<T>.Insert(T objT) { return this.Insert(objT); } #endregion #region IDelete<T> 成員 int IDelete<T>.Delete() { return this.Delete(); } int IDelete<T>.Delete(T objT) { return this.Delete(objT); } #endregion #region IUpdate<T> 成員 int IUpdate<T>.Update(T objT) { return this.Update(objT); } #endregion }介面 /// <!--Insert介面--> /// <summary> /// Insert介面 - design By Phoenix 2008 - /// </summary> /// <typeparam name="T">資料物件</typeparam> public interface IInsert<T> { ///<!--新增一筆資料行--> /// <summary> /// 新增一筆資料行 - design By Phoenix 2008 - /// </summary> /// <param name="objT">資料物件</param> /// <returns>受影響的行數</returns> int Insert(T objT); } /// <!--Delete介面--> /// <summary> /// Delete介面 - design By Phoenix 2008 - /// </summary> /// <typeparam name="T">資料物件</typeparam> public interface IDelete<T> { ///<!--執行刪除的動作,刪除Del標記為true的項目--> /// <summary> /// 執行刪除的動作,刪除Del標記為true的項目 - design By Phoenix 2008 - /// </summary> /// <returns>受影響的行數</returns> int Delete(); ///<!--設定資料刪除標記--> /// <summary> /// 設定資料刪除標記 - design By Phoenix 2008 - /// </summary> /// <param name="objT">資料物件</param> /// <returns>受影響的行數</returns> int Delete(T objT); } /// <!--Update介面 --> /// <summary> /// Update介面 - design By Phoenix 2008 - /// </summary> /// <typeparam name="T">資料物件</typeparam> public interface IUpdate<T> { ///<!--修改資料行--> /// <summary> /// 修改資料行 - design By Phoenix 2008 - /// </summary> /// <param name="objT">資料物件</param> /// <returns>受影響的行數</returns> int Update(T objT); }Activity元件 ///<!--Activity資料表,商業邏輯元件--> /// <summary> /// Activity資料表,商業邏輯元件 - design By Phoenix 2008 - /// </summary> public class Activity:GenericBase<ActivityOM> { //宣告資料存取元件(已於Base宣告) //private MySqlServer objData; ///<!--建立Activity物件--> /// <summary> /// 建立Activity物件 - design By Phoenix 2008 - /// </summary> /// <param name="row">DataRow(資料行)</param> /// <returns>Activity物件</returns> protected override ActivityOM CreateObject(DataRow row) { //例外處理 try { //Object and Collection Initializers return new ActivityOM() { ActID = (int)row[0], ActDate = (DateTime)row[1], ActSubject = row[2].ToString(), ActTime = row[3].ToString(), ActContent = row[4].ToString(), ActAction = (bool)row[5], Del = (bool)row[6], Chger = (string)row[7], ChgTime = (DateTime)row[8] }; } catch { //擲回例外 throw new InvalidOperationException("CreateObject區段執行失敗<參數錯誤>"); } } ///<!--建立Insert強型別物件清單--> /// <summary> /// 建立Insert強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="oAct">Activity物件</param> /// <returns>DataField強型別物件清單</returns> protected override List<DataField> CreateInsertList(ActivityOM oAct) { //例外處理 try { //判斷Chger(異動者)欄位內容是否為null if (oAct.Chger == null) //若為null //擲回例外。 throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。"); else { //建立回傳暫存(DataField強型別物件清單) List<DataField> result = new List<DataField>(); result.Clear(); //將要寫入的欄位、值 加入清單 result.Add(new DataField("ActSubject", oAct.ActSubject)); result.Add(new DataField("ActActTime", oAct.ActTime)); result.Add(new DataField("ActContent", oAct.ActContent)); result.Add(new DataField("ActAction", oAct.ActAction)); result.Add(new DataField("Chger", oAct.Chger)); //回傳結果 return result; } } catch { //擲回例外 throw new InvalidOperationException("CreateInsertList區段執行失敗<參數錯誤>"); } } ///<!--建立Delete強型別物件清單--> /// <summary> /// 建立Delete強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="oAct">Activity物件</param> /// <returns>DataField強型別物件清單</returns> protected override List<DataField> CreateDeleteList(ActivityOM oAct) { //例外處理 try { //建立回傳暫存(DataField強型別物件清單) List<DataField> result = new List<DataField>(); result.Clear(); //加入條件式 result.Add(new DataField("Original_ActID", oAct.ActID)); //加入刪除標記 result.Add(new DataField("Del", oAct.Del)); //回傳結果 return result; } catch { //擲回例外 throw new InvalidOperationException("CreateDeleteList區段執行失敗<參數錯誤>"); } } ///<!--建立Update強型別物件清單--> /// <summary> /// 建立Update強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="oAct">Activity物件</param> /// <returns>DataField強型別物件清單</returns> protected override List<DataField> CreateUpdateList(ActivityOM oAct) { //例外處理 try { //判斷Chger(異動者)欄位內容是否為null if (oAct.Chger == null) //若為null //擲回例外 throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。"); else { //建立回傳暫存(DataField強型別物件清單) List<DataField> result = new List<DataField>(); result.Clear(); //加入(Original)條件限制 result.Add(new DataField("Original_ActID", oAct.ActID)); //加入新的值 result.Add(new DataField("ActSubject", oAct.ActSubject)); result.Add(new DataField("ActTime", oAct.ActTime)); result.Add(new DataField("ActContent", oAct.ActContent)); result.Add(new DataField("ActAction", oAct.ActAction)); result.Add(new DataField("Chger", oAct.Chger)); result.Add(new DataField("ChgTime", DateTime.Now)); //回傳結果 return result; } } catch { //擲回例外 throw new InvalidOperationException("CreateUpdateList區段執行失敗<參數錯誤>"); } } /// <!--取得Activity強型別物件清單(ByActivityID)--> /// <summary> /// 取得Activity強型別物件清單(ByActivityID) - design By Phoenix 2008 - /// </summary> /// <param name="AID"></param> /// <returns></returns> public List<ActivityOM> GetBy(int AID) { //例外處理 try { return GetBy(CreateCondition(AID)); } catch (Exception) { //擲出例外 throw; } } private List<DataField> CreateCondition(int AID) { return new List<DataField>() { new DataField("ActID", AID) }; } ///<!--初始化 BusinessLogicLayer.Activity 類別的新執行個體--> /// <summary> /// 初始化 BusinessLogicLayer.Activity 類別的新執行個體 - design By Phoenix 2008 - /// </summary> public Activity():base(DbName.Activity) { //初始化 objData objData = new MySqlServer(); } }News元件 ///<!--News資料表,商業邏輯元件--> /// <summary> /// News資料表,商業邏輯元件 - design By Phoenix 2008 - /// </summary> public class News:GenericBase<NewsOM> { //宣告資料存取元件(已於Base宣告) //private MySqlServer objData; /// <!--建立News物件--> /// <summary> /// 建立News物件 - design By Phoenix 2008 - /// </summary> /// <param name="row">資料行</param> /// <returns>News物件</returns> protected override NewsOM CreateObject(DataRow row) { //例外處理 try { //Object and Collection Initializers return new NewsOM() { NewsID = (int)row[0], NewsDate = (DateTime)row[1], NewsSubject = row[2].ToString(), NewsContent = row[3].ToString(), Del = (bool)row[4], Chger = row[5].ToString(), ChgTime = (DateTime)row[6] }; } catch { //擲回例外 throw new InvalidOperationException("CreateObject區段執行失敗<參數錯誤>"); } } ///<!--建立Insert強型別物件清單--> /// <summary> /// 建立Insert強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="oNews">News物件</param> /// <returns>DataField強型別物件清單</returns> protected override List<DataField> CreateInsertList(NewsOM oNews) { //例外處理 try { //判斷Chger(異動者)欄位內容是否為null if (oNews.Chger == null) //若為null //擲回例外。 throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。"); else { //若不為null //建立回傳暫存(DataField強型別物件清單) List<DataField> result = new List<DataField>(); result.Clear(); //將要寫入的欄位、值 加入清單 result.Add(new DataField("NewsSubject", oNews.NewsSubject)); result.Add(new DataField("NewsContent", oNews.NewsContent)); result.Add(new DataField("Chger", oNews.Chger)); //回傳結果 return result; } } catch { //擲回例外 throw new InvalidOperationException("CreateInsertList區段執行失敗<參數錯誤>"); } } ///<!--建立Delete強型別物件清單--> /// <summary> /// 建立Delete強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="oNews">News物件</param> /// <returns>DataField強型別物件清單</returns> protected override List<DataField> CreateDeleteList(NewsOM oNews) { //例外處理 try { //建立回傳暫存(DataField強型別物件清單) List<DataField> result = new List<DataField>(); result.Clear(); //加入條件式 result.Add(new DataField("Original_NewsID", oNews.NewsID)); //加入刪除標記 result.Add(new DataField("Del", oNews.Del)); //回傳結果 return result; } catch { //擲回例外 throw new InvalidOperationException("CreateDeleteList區段執行失敗<參數錯誤>"); } } ///<!--建立Update強型別物件清單--> /// <summary> /// 建立Update強型別物件清單 - design By Phoenix 2008 - /// </summary> /// <param name="oNews">News物件</param> /// <returns>DataField強型別物件清單</returns> protected override List<DataField> CreateUpdateList(NewsOM oNews) { //例外處理 try { //判斷Chger(異動者)欄位內容是否為null if (oNews.Chger == null) //若為null //擲回例外 throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。"); else { //若不為null //建立回傳暫存(DataField強型別物件清單) List<DataField> result = new List<DataField>(); result.Clear(); //加入(Original)條件限制 result.Add(new DataField("Original_NewsID", oNews.NewsID)); //加入新的值 result.Add(new DataField("NewsSubject", oNews.NewsSubject)); result.Add(new DataField("NewsContent", oNews.NewsContent)); result.Add(new DataField("Chger", oNews.Chger)); result.Add(new DataField("ChgTime", DateTime.Now)); //回傳結果 return result; } } catch { //擲回例外 throw new InvalidOperationException("CreateUpdateList區段執行失敗<參數錯誤>"); } } /// <!--取得News強型別物件清單(ByNewsID)--> /// <summary> /// 取得News強型別物件清單(ByNewsID) - design By Phoenix 2008 - /// </summary> /// <param name="NID"></param> /// <returns></returns> public List<NewsOM> GetBy(int NID) { //例外處理 try { return GetBy(CreateCondition(NID)); } catch (Exception) { //擲出例外 throw; } } private List<DataField> CreateCondition(int NID) { return new List<DataField>() { new DataField("NewsID", NID) }; } ///<!--初始化 BusinessLogicLayer.News 類別的新執行個體--> /// <summary> /// 初始化 BusinessLogicLayer.News 類別的新執行個體 - design By Phoenix 2008 - /// </summary> private News():base(DbName.News) { //初始化 objData objData = new MySqlServer(); } } 以上Phoenix 9/3 ADO.NET程式架構.NET 回首頁