對於解決需要實現某些重寫函數的每個業務對象列表的問題的最佳方法,我有點模糊。我該如何最好地創建一組列表類以匹配我的業務對象
這裏的設置: 我有一個baseObject,設置了數據庫,並有適當的Dispose()方法 我所有的其他業務對象繼承它,並在必要時,重寫的Dispose()
一些這些類還包含其他對象的數組(列表)。所以我創建了一個擁有這些列表的類。我知道我可以使用泛型列表,但這不會讓我添加像Dispose()這樣的額外功能,因此它將循環並清理。
所以如果我有名爲User,Project和Schedule的對象,我會創建UserList,ProjectList,ScheduleList。
在過去,我只是將這些繼承自列表<>,並將其命名爲適當的類,然後編寫了一堆我希望它具有的常用函數,如Dispose()。
這意味着我會手工驗證每個這些List類具有相同的一組方法。其中一些類具有非常簡單的版本,可以從基本列表類繼承。
我可以編寫一個接口,強制我確保每個List類具有相同的函數,但接口不允許我編寫一些列表可能覆蓋的通用基函數。我試圖寫一個從列表繼承的baseObjectList,然後讓我的其他列表繼承,但也有問題(這真的是爲什麼我來到這裏)。其中之一就是試圖對謂詞使用Find()方法。
我簡化了這個問題,只是簡單地討論了Dispose()方法,它通過循環並處理其內容,但事實上,我還有其他幾個常用函數,我希望我的所有列表都具有。
解決此組織問題的最佳做法是什麼?
--------編輯---添加了一個示例類,我試圖用更好的方法清理 - 下一個代碼塊來自我的AppVarList類,它直接從List繼承。現在,我已經有了對這些列表風格的混合,因爲我試圖找到管理所有這些和鞏固代碼的最佳方式:
[Serializable]
public class AppVarList : List<AppVar>
{
private bool is_disposed = false;
private bool _NeedsSaving = false;
// Returns the AppVar, by name
public AppVar this[string index]
{
get { return this.Find(f => f.Key == index); }
set { this[this.FindIndex(f => f.Key == index)] = value; }
}
public AppVarList() { }
// instantiates the object and connects it to the DB
// and populates it
internal AppVarList(ref DBLib.DBAccess DA)
{
DataSet ds =
AppVar.SQLSelect(ref DA, 0, 0, false, "", "", "", "");
foreach (DataRow dr in ds.Tables[0].Rows)
{
this.Add(new AppVar(ref DA, dr));
}
// Destroy the dataset object
if (ds != null)-
讓我們來看看一些代碼...我想你可能會得太多這個。你可能有一個帶有自定義方法的列表類,但是如果這些自定義方法總是相同的話,你應該只需要一個通用列表類。 – 2010-05-21 21:57:13
基本上,我有類似的列表類,在各種實現中,我真的想將常見行爲合併到某種基本列表類中,然後只在我真的需要一個類的特殊行爲時繼承。 AppVar就是一個例子,因爲它就像一個iDictionary(我只是最近才找到的,應該已經使用過......) – KenF 2010-05-21 22:39:27
您寫了一個關於在.NET對象中篡改數據庫行並將其放入集合的長篇故事。你不應該僅僅使用一個O/RM工具,比如LINQ to SQL或者實體框架嗎?這可能會解決很多問題。例如,你爲什麼要在每個對象上實現Dispose? – Steven 2010-05-21 22:51:09