C#3.0 初步階段修正 DAL BLL

初步修正 ...程式碼皆以加上註解 ... 故 不再說明(還是以 新聞公告為範例)

初步修正 ...程式碼皆以加上註解 ... 故 不再說明(還是以 新聞公告為範例)

DOM 物件模型

01 using System;
02
03 namespace DOM
04 {
05     public class News
06     {
07         public int NewsID { get; set; }
08         public DateTime NewsDate { get; set; }
09         public string NewsSubject { get; set; }
10         public string NewsContent { get; set; }
11         public bool Del { get; set; }
12         public string Chger { get; set; }
13         public DateTime ChgTime { get; set; }
14     }

15 }

 


DAL資料存取層

DataField 物件用來紀錄欄位名稱和欄位值

01 namespace DAL
02 {
03     ///<!--DataField物件模型-->
04     /// <summary>
05     /// DataField物件模型 - design By Phoenix 2008
06     /// </summary>

07     public class DataField
08     {
09         ///<!--ColumnName 屬性-->
10         /// <summary>
11         /// ColumnName 屬性 - design By Phoenix 2008
12         /// </summary>
13         public string ColumnName { get; set; }
14
15         ///<!--ColumnValue 屬性-->
16         /// <summary>
17         /// ColumnValue 屬性 - design By Phoenix 2008
18         /// </summary>
19         public object ColumnValue { get; set; }
20
21         ///<!--初始化 DAL.DataField 類別的新執行個體-->
22         /// <summary>
23         /// 初始化 DAL.DataField 類別的新執行個體 - design By Phoenix 2008
24         /// </summary>
25         public DataField() { }
26
27         ///<!--初始化 DAL.DataField 類別的新執行個體-->
28         /// <summary>
29         /// 初始化 DAL.DataField 類別的新執行個體 - design By Phoenix 2008
30         /// </summary>
31         /// <param name="Name">ColumnName 屬性的值</param>
32         /// <param name="Value">ColumnValue 屬性的值</param>

33         public DataField(string Name, object Value)
34         {
35             //寫入新的值
36             ColumnName = Name;
37             ColumnValue = Value;
38         }

39     }

40 }

 


DAL.ConnectionString

連線字串元件

 

DAL.DataObject ... 600多行 ...只是幾乎一半以上都是註解 ...

資料存取層核心元件

BLL

001 using System;
002 using System.Collections.Generic;
003 using System.Data;
004 using DAL.DataObject;

005
006 namespace BLL
007 {
008     ///<!--News資料表,商業邏輯元件-->
009     /// <summary>
010     /// News資料表,商業邏輯元件 - design By Phoenix 2008 -
011     /// </summary>

012     public class News
013     {
014         //宣告資料存取元件
015         private DAL.DataObject.MySqlServer objData;
016         
017         /// <!--建立News物件-->
018         /// <summary>
019         /// 建立News物件 - design By Phoenix 2008 -
020         /// </summary>
021         /// <param name="row">資料行</param>
022         /// <returns>News物件</returns>

023         private DOM.News CreateObject(DataRow row)
024         {
025             //例外處理
026             try
027             {
028                 //Object and Collection Initializers
029                 return new DOM.News()
030                 {
031                     NewsID = (int)row[0],
032                     NewsDate = (DateTime)row[1],
033                     NewsSubject = row[2].ToString(),
034                     NewsContent = row[3].ToString(),
035                     Del = (bool)row[4],
036                     Chger = row[5].ToString(),
037                     ChgTime = (DateTime)row[6]
038                 }
;
039             }

040             catch
041             {
042                 //擲回例外
043                 throw new InvalidOperationException("CreateObject區段執行失敗<參數錯誤>");
044             }

045         }

046
047         ///<!--由DataTable(資料表)轉換成List(強型別物件清單)-->
048         /// <summary>
049         /// 由DataTable(資料表)轉換成List(強型別物件清單) - design By Phoenix 2008 -
050         /// </summary>
051         /// <param name="dt">要轉換的DataTable(資料表)</param>
052         /// <returns>News強型別物件清單</returns>

053         private List<DOM.News> ToList(DataTable dt)
054         {
055             //例外處理
056             try
057             {
058                 //建立回傳暫存(News強型別物件清單)
059                 List<DOM.News> result = new List<DOM.News>();
060                 result.Clear();
061                 //將DataTable一一取出
062                 //呼叫CreateObject(傳入資料行)(傳回News物件)
063                 //將News物件加入到回傳暫存
064                 foreach (DataRow row in dt.Rows)
065                     result.Add(CreateObject(row));
066                 //回傳結果
067                 return result;
068             }

069             catch
070             {
071                 //擲回例外
072                 throw new InvalidOperationException("ToList區段執行失敗<參數錯誤>");
073             }

074         }

075
076         ///<!--建立Insert強型別物件清單-->
077         /// <summary>
078         /// 建立Insert強型別物件清單 - design By Phoenix 2008 -
079         /// </summary>
080         /// <param name="oNews">News物件</param>
081         /// <returns>DataField強型別物件清單</returns>

082         private List<DAL.DataField> CreateInsertList(DOM.News oNews)
083         {
084             //例外處理
085             try
086             {
087                 //建立回傳暫存(DataField強型別物件清單)
088                 List<DAL.DataField> result = new List<DAL.DataField>();
089                 result.Clear();
090                 //將要寫入的欄位、值 加入清單
091                 result.Add(new DAL.DataField("NewsSubject", oNews.NewsSubject));
092                 result.Add(new DAL.DataField("NewsContent", oNews.NewsContent));
093                 result.Add(new DAL.DataField("Chger", oNews.Chger));
094                 //回傳結果
095                 return result;
096             }

097             catch
098             {
099                 //擲回例外
100                 throw new InvalidOperationException("CreateInsertList區段執行失敗<參數錯誤>");
101             }

102         }

103
104         ///<!--建立Delete強型別物件清單-->
105         /// <summary>
106         /// 建立Delete強型別物件清單 - design By Phoenix 2008 -
107         /// </summary>
108         /// <param name="oNews">News物件</param>
109         /// <returns>DataField強型別物件清單</returns>

110         private List<DAL.DataField> CreateDeleteList(DOM.News oNews)
111         {
112             //例外處理
113             try
114             {
115                 //建立回傳暫存(DataField強型別物件清單)
116                 List<DAL.DataField> result = new List<DAL.DataField>();
117                 result.Clear();
118                 //加入條件式
119                 result.Add(new DAL.DataField("Original_NewsID", oNews.NewsID));
120                 //加入刪除標記
121                 result.Add(new DAL.DataField("Del", oNews.Del));
122                 //回傳結果
123                 return result;
124             }

125             catch
126             {
127                 //擲回例外
128                 throw new InvalidOperationException("CreateDeleteList區段執行失敗<參數錯誤>");
129             }

130         }

131
132         ///<!--建立Update強型別物件清單-->
133         /// <summary>
134         /// 建立Update強型別物件清單 - design By Phoenix 2008 -
135         /// </summary>
136         /// <param name="oNews">News物件</param>
137         /// <returns>DataField強型別物件清單</returns>

138         private List<DAL.DataField> CreateUpdateList(DOM.News oNews)
139         {
140             //例外處理
141             try
142             {
143                 //建立回傳暫存(DataField強型別物件清單)
144                 List<DAL.DataField> result = new List<DAL.DataField>();
145                 result.Clear();
146                 //加入(Original)條件限制
147                 result.Add(new DAL.DataField("Original_NewsID", oNews.NewsID));
148                 //加入新的值
149                 result.Add(new DAL.DataField("NewsSubject", oNews.NewsSubject));
150                 result.Add(new DAL.DataField("NewsContent", oNews.NewsContent));
151                 result.Add(new DAL.DataField("Chger", oNews.Chger));
152                 result.Add(new DAL.DataField("ChgTime", DateTime.Now));
153                 //回傳結果
154                 return result;
155             }

156             catch
157             {
158                 //擲回例外
159                 throw new InvalidOperationException("CreateUpdateList區段執行失敗<參數錯誤>");
160             }

161         }

162
163         /// <!--取得所有News強型別物件清單-->
164         /// <summary>
165         /// 取得所有News強型別物件清單 - design By Phoenix 2008 -
166         /// </summary>
167         /// <returns></returns>

168         public List<DOM.News> GetAll()
169         {
170             //例外處理
171             try
172             {
173                 //field(Select欄位集合),*為取得所有欄位
174                 string[] field = new string[] { "*" };
175                 //執行Select介面,傳入Select欄位集合、資料表名稱,傳回資料表
176                 DataTable dt = ((iSelect)objData).GetTable(field, DAL.DbName.News);
177                 //將資料表轉換成強型別物件清單再回傳
178                 return ToList(dt);
179             }

180             catch (Exception)
181             {
182                 //擲出例外
183                 throw;
184             }

185         }

186
187         /// <!--取得News強型別物件清單-->
188         /// <summary>
189         /// 取得News強型別物件集合 - design By Phoenix 2008 -
190         /// </summary>
191         /// <param name="Condition">條件限制清單</param>
192         /// <returns>News強型別物件清單</returns>

193         public List<DOM.News> GetBy(List<DAL.DataField> Condition)
194         {
195             //例外處理
196             try
197             {
198                 //field(Select欄位集合),*為取得所有欄位
199                 string[] field = new string[] { "*" };
200                 //執行Select介面,傳入Select欄位集合、資料表名稱、條件限制清單,傳回資料表
201                 DataTable dt = ((iSelect)objData).GetTable(field, DAL.DbName.News, Condition);
202                 //將資料表轉換成強型別物件清單再回傳
203                 return ToList(dt);
204             }

205             catch (Exception)
206             {
207                 //擲出例外
208                 throw;
209             }

210         }

211
212         /// <!--新增一筆News資料行-->
213         /// <summary>
214         /// 新增一筆News資料行 - design By Phoenix 2008 -
215         /// </summary>
216         /// <param name="oNews">News物件</param>
217         /// <returns>受影響的行數</returns>

218         public int Insert(DOM.News oNews)
219         {
220             //例外處理
221             try
222             {
223                 //判斷Chger(異動者)欄位內容是否為null
224                 if (oNews.Chger == null)
225                     //若為null
226                     //擲回例外。
227                     throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。");
228                 else
229                     //若不為null
230                     //執行CreateInsertList(傳入News清單)(傳回Insert清單)
231                     //執行Insert介面(傳入資料表名稱、Insert清單)(傳回影響的行數)
232                     //傳回受影響的行數
233                     return ((iInsert)objData).Execute(DAL.DbName.News, CreateInsertList(oNews));
234             }

235             catch(Exception)
236             {
237                 //擲出例外
238                 throw;
239             }

240         }

241
242         /// <!--執行刪除的動作,刪除Del標記為true的項目-->
243         /// <summary>
244         /// 執行刪除的動作,刪除Del標記為true的項目 - design By Phoenix 2008 -
245         /// </summary>
246         /// <returns>受影響的行數</returns>

247         public int Delete()
248         {
249             //例外處理
250             try
251             {
252                 //自訂SqlQuery查詢指令
253                 string SqlQuery = "DELETE FROM News WHERE (Del = 1)";
254                 //執行Query,傳回受影響的行數
255                 return objData.RunQuery(SqlQuery);
256             }

257             catch (Exception)
258             {
259                 //擲出例外
260                 throw;
261             }

262         }

263
264         /// <!--設定News刪除標記-->
265         /// <summary>
266         /// 設定News刪除標記 - design By Phoenix 2008 -
267         /// </summary>
268         /// <param name="oNews">News物件</param>
269         /// <returns>受影響的行數</returns>

270         public int Delete(DOM.News oNews)
271         {
272             //例外處理
273             try
274             {
275                 //呼叫CreateDeleteList(傳入News物件)(傳回Delete清單)
276                 //執行Update介面(傳入資料表名稱、Delete清單)(傳回受影響的行數)
277                 //傳回受影響的行數
278                 return ((iUpdate)objData).Execute(DAL.DbName.News, CreateDeleteList(oNews));
279             }

280             catch
281             {
282                 //擲出例外
283                 throw;
284             }

285         }

286
287         /// <!--修改News資料行-->
288         /// <summary>
289         /// 修改News資料行 - design By Phoenix 2008 -
290         /// </summary>
291         /// <param name="oNews"></param>
292         /// <returns></returns>

293         public int Update(ref DOM.News oNews)
294         {
295             //例外處理
296             try
297             {
298                 //判斷Chger(異動者)欄位內容是否為null
299                 if (oNews.Chger == null)
300                     //若為null
301                     //擲回例外
302                     throw new InvalidOperationException("未經許可的操作!如有疑問,請洽程式人員。");
303                 else
304                     //若不為null
305                     //呼叫CreateUpdateList(傳入News物件)(傳回Update清單)
306                     //執行Update介面(傳入資料表名稱、Update清單)(傳回影響的行數)
307                     //傳回受影響的行數
308                     return ((iUpdate)objData).Execute(DAL.DbName.News, CreateUpdateList(oNews));
309             }

310             catch (Exception)
311             {
312                 //擲出例外
313                 throw;
314             }

315         }

316
317         ///<!--初始化 BLL.News 類別的新執行個體-->
318         /// <summary>
319         /// 初始化 BLL.News 類別的新執行個體 - design By Phoenix 2008 -
320         /// </summary>

321         private News()
322         {
323             //初始化 objData
324             objData = new DAL.DataObject.MySqlServer();
325         }

326     }

327 }

初步測試,沒什麼問題

以上

Phoenix 8/20