2010-05-21 23 views
1

對於解決需要實現某些重寫函數的每個業務對象列表的問題的最佳方法,我有點模糊。我該如何最好地創建一組列表類以匹配我的業務對象

這裏的設置: 我有一個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)- 
+2

讓我們來看看一些代碼...我想你可能會得太多這個。你可能有一個帶有自定義方法的列表類,但是如果這些自定義方法總是相同的話,你應該只需要一個通用列表類。 – 2010-05-21 21:57:13

+0

基本上,我有類似的列表類,在各種實現中,我真的想將常見行爲合併到某種基本列表類中,然後只在我真的需要一個類的特殊行爲時繼承。 AppVar就是一個例子,因爲它就像一個iDictionary(我只是最近才找到的,應該已經使用過......) – KenF 2010-05-21 22:39:27

+0

您寫了一個關於在.NET對象中篡改數據庫行並將其放入集合的長篇故事。你不應該僅僅使用一個O/RM工具,比如LINQ to SQL或者實體框架嗎?這可能會解決很多問題。例如,你爲什麼要在每個對象上實現Dispose? – Steven 2010-05-21 22:51:09

回答

0

你應該繼承一個共同的通用基礎類從System.Collections.ObjectModel.Collection<T>

+0

然後.......什麼? – 2010-05-21 21:57:33

+1

然後,您可以將您的方法添加到基礎,也許是虛擬的,以便您可以覆蓋它們,並且可以使特定的類從基礎繼承,但是對於特定的類型。例如ProjectList:BaseClass 2010-05-21 22:07:22

+0

so,make a baseList as class baseList :System.Collections.ObjectModel.Collection {// stuff here,maybe as virtual}然後對於我的特定列表類class ProjectList:baseList {//覆蓋東西我想重寫} – KenF 2010-05-21 22:24:44

0

你最好的選擇是擴展方法添加到您的IEnumerable集合即

public static ScheduleListExtentions 
{ 
    public static IEnumerable<ScheduleList> DoSomething(this IEnumerable<ScheduleList> mylist){ 
     //Act on your collection in some mannor here 
     return mylist; 
    } 
} 
1

你能肯定你的一些功能,拉成一個基類:

[Serializable] 
public class BaseDataList<T> : List<T> 
    where T : BaseObject // this is the key line, it ensure that 
         // T is a BaseObject so you can call 
         //BaseObject methods on T, should allow for you 
         //to put more implementation in this class than 
         // you could otherwise 
{ 
    private bool is_disposed = false; 
    private bool _NeedsSaving = false; 

    ///this assumes that key is a property on BaseObject that everything implements 
    public T this[string index] 
    { 
     get { return this.Find(f => f.Key == index); } 
     set { this[this.FindIndex(f => f.Key == index)] = value; } 
    } 

    public BaseDataList() { } 

    // instantiates the object and connects it to the DB 
    // and populates it 
    internal BaseDataList(ref DBLib.DBAccess DA) 
    { 
     DataSet ds = GetDataSet(); 

     foreach (DataRow dr in ds.Tables[0].Rows) 
     { 
      this.Add(new T(ref DA, dr)); 
     } 
     // Destroy the dataset object 
     if (ds != null)//... 
    } 
    //abstract methods for extensibility... 
    protected abstract GetDataSet(); 
} 

顯然這僅僅是一個粗略的草圖,因爲我不知道所有的需求,但根據BaseObject中共享多少實現,您可能會將許多功能放入BaseDataList(您應該隨時選擇更好的名稱)。那麼所有其他列表就可以從BaseDataList派生。

例如,如果BaseObject實現IDisposable,你可以將這個方法添加到您的類

protected void Dispose() 
{ 
    is_disposing = true; 
    foreach(T item in this) 
    { 
     item.Dispose(); 
    } 
} 

那麼你就只需要編寫一次

+0

這個釘牢它。我現在正在清理垃圾。 我不知道我可以把一個WHERE T:baseObject作爲泛型繼承的一部分。看到這種語法使得這一切顯而易見。 謝謝 – KenF 2010-05-22 15:43:18

+0

@ ken-forslund沒有說感謝就像一個複選標記和表決:) – luke 2010-05-22 17:26:33

相關問題