C# 如何最好地创建一组列表类来匹配我的业务对象

C# 如何最好地创建一组列表类来匹配我的业务对象,c#,inheritance,list,collections,generics,C#,Inheritance,List,Collections,Generics,对于解决需要为实现某些重写函数的每个业务对象创建一个列表的问题的最佳方法,我有点模糊 以下是设置: 我有一个设置数据库的baseObject,并有其适当的Dispose()方法 我的所有其他业务对象都继承自它,如有必要,重写Dispose() 其中一些类还包含其他对象的数组(列表)。因此,我创建了一个类,其中包含这些元素的列表。我知道我可以只使用泛型列表,但这不允许我添加额外的功能,如Dispose(),这样它将循环并清理 所以,如果我有名为User、Project和Schedule的对象,我将

对于解决需要为实现某些重写函数的每个业务对象创建一个列表的问题的最佳方法,我有点模糊

以下是设置: 我有一个设置数据库的baseObject,并有其适当的Dispose()方法 我的所有其他业务对象都继承自它,如有必要,重写Dispose()

其中一些类还包含其他对象的数组(列表)。因此,我创建了一个类,其中包含这些元素的列表。我知道我可以只使用泛型列表,但这不允许我添加额外的功能,如Dispose(),这样它将循环并清理

所以,如果我有名为User、Project和Schedule的对象,我将创建UserList、ProjectList、ScheduleList

在过去,我只是让这些函数从列表中继承,并命名相应的类,然后编写我希望它拥有的一堆公共函数,如Dispose()

这意味着我将手工验证这些列表类中的每一个都有相同的方法集。其中一些类具有这些方法的非常简单的版本,可以从基列表类继承

我可以编写一个接口,强制我确保我的每个列表类都有相同的函数,但接口不允许我编写一些列表可能覆盖的公共基函数

我曾试图编写一个从列表继承的baseObjectList,然后让我的其他列表从该列表继承,但存在一些问题(这就是我来到这里的真正原因)。其中之一是尝试将Find()方法与谓词一起使用

我已经将问题简化为对列表上Dispose()方法的讨论,该方法循环并处理其内容,但实际上,我有几个其他常用函数,我希望所有列表都有这些函数

解决这个组织问题的最佳实践是什么

--------edit--添加了一个示例类,我正试图用更好的方法清理它--下一个代码块来自我的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)-
[可序列化]
公共类AppVarList:List
{
private bool是_disposed=false;
私有布尔_NeedsSaving=false;
//按名称返回AppVar
公共AppVar此[字符串索引]
{
get{返回this.Find(f=>f.Key==index);}
设置{this[this.FindIndex(f=>f.Key==index)]=value;}
}
公共AppVarList(){}
//实例化对象并将其连接到数据库
//并将其填充
内部AppVarList(参考DBLib.DBAccess DA)
{
数据集ds=
SQLSelect(refda,0,0,false,“”,“”,“”,“”,“”);
foreach(ds.Tables[0].行中的数据行dr)
{
添加(新的AppVar(参考DA,dr));
}
//销毁数据集对象
如果(ds!=null)-

您应该创建一个从
System.Collections.ObjectModel.Collection继承的公共泛型基类
您应该创建一个从
System.Collections.ObjectModel.Collection继承的公共泛型基类
您最好的选择是向IEnumerable集合添加扩展方法,即

public static ScheduleListExtentions
{
    public static IEnumerable<ScheduleList> DoSomething(this IEnumerable<ScheduleList> mylist){
       //Act on your collection in some mannor here
       return mylist;
    } 
}
publicstaticschedulelistextensions
{
公共静态IEnumerable剂量测量(此IEnumerable mylist){
//以某种方式对您的收藏进行操作
返回mylist;
} 
}

您的最佳选择是向IEnumerable集合添加扩展方法,即

public static ScheduleListExtentions
{
    public static IEnumerable<ScheduleList> DoSomething(this IEnumerable<ScheduleList> mylist){
       //Act on your collection in some mannor here
       return mylist;
    } 
}
publicstaticschedulelistextensions
{
公共静态IEnumerable剂量测量(此IEnumerable mylist){
//以某种方式对您的收藏进行操作
返回mylist;
} 
}

您当然可以将一些功能拉入基类:

[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();
}

然后您只需编写一次

您当然可以将一些功能拉入基类:

[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();
}

那么您只需编写一次

让我们看看一些代码……我想您可能想得太多了。您可能有一个带有自定义方法的列表类,但如果这些自定义方法总是相同的,您应该只需要一个通用列表类。基本上,我有这样的列表类,在各种实现中,我真正想要的是将常见行为合并到某种基列表类中,然后仅当我真的需要一个类的特殊行为时才继承。AppVar就是一个例子,因为它的行为类似于iDictionary(我最近才发现,应该使用它…)您写了一篇关于将数据库行封装在.NET对象中并将其放入集合的长篇故事。您不应该为此使用O/RM工具(如LINQ to SQL或Entity Framework)吗?这可能会解决很多问题。例如,为什么要在每个对象上实现
Dispose
?遗留开发样式。I我一直想查看LINQ,看看它能为我做些什么。但缺点是,我们从.NET 1.0开始就一直这样做。每个对象上的Dispose()都是因为每个对象都带有数据库连接(实际上是指向它的指针/引用).我不能在这个项目上换人,但我可以做一些代码清理,去掉一些“笨蛋”解决方案。至于使用LINQ或类似的方法,这是一个单独的计划,希望我可以在下一个项目中使用。让我们看看一些代码…我想你可能想得太多了。你可能有一个带有自定义方法的列表类,但如果这些自定义方法总是相同的,你应该只需要一个通用列表类。基本上,我有列表类就像这样,在各种实现中,我真的希望将常见行为合并到某种基列表类中,然后仅当我真的需要一个类的特殊行为时才继承