如何最好地创建一组列表类来匹配我的业务对象
我对解决每个实现某些重写功能的业务对象需要一个列表的问题的最佳方法有点模糊。
这是设置: 我有一个用于设置数据库的 baseObject,并具有其正确的 Dispose() 方法 我的所有其他业务对象都继承自它,并且如果需要的话,重写 Dispose()
其中一些类还包含其他对象的数组(列表)。所以我创建了一个包含这些列表的类。我知道我可以只使用通用列表,但这不允许我添加像 Dispose() 这样的额外功能,因此它会循环并清理。
因此,如果我有名为 User、Project 和 Schedule 的对象,我将创建 UserList、ProjectList、ScheduleList。
过去,我只是从 List<> 继承了这些。命名适当的类,然后编写我希望它具有的一堆常用函数,例如 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)-
I'm a bit fuzzy on the best way to solve the problem of needing a list for each of my business objects that implements some overridden functions.
Here's the setup:
I have a baseObject that sets up database, and has its proper Dispose() method
All my other business objects inherit from it, and if necessary, override Dispose()
Some of these classes also contain arrays (lists) of other objects. So I create a class that holds a List of these. I'm aware I could just use the generic List, but that doesn't let me add extra features like Dispose() so it will loop through and clean up.
So if I had objects called User, Project and Schedule, I would create UserList, ProjectList, ScheduleList.
In the past, I have simply had these inherit from List<> with the appropriate class named and then written the pile of common functions I wanted it to have, like Dispose().
this meant I would verify by hand, that each of these List classes had the same set of methods. Some of these classes had pretty simple versions of these methods that could have been inherited from a base list class.
I could write an interface, to force me to ensure that each of my List classes has the same functions, but interfaces don't let me write common base functions that SOME of the lists might override.
I had tried to write a baseObjectList that inherited from List, and then make my other Lists inherit from that, but there are issues with that (which is really why I came here). One of which was trying to use the Find() method with a predicate.
I've simplified the problem down to just a discussion of Dispose() method on the list that loops through and disposes its contents, but in reality, I have several other common functions that I want all my lists to have.
What's the best practice to solve this organizational matter?
--------edit---added an example class that I'm trying to clean up with a better approach --This next code block is from my AppVarList class, which inherits straight from List. Right now I've got a mixture of styles on these lists, because I'm trying to find the best way to manage all these and consolidate the code:
[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)-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您当然可以将一些功能提取到基类中:
显然这只是一个粗略的草图,因为我不知道您的所有要求,但是根据 BaseObject 中共享的实现量,您可能可以放置很多将功能添加到 BaseDataList 中(您应该随意选择一个更好的名称)。那么所有其他列表都可以从 BaseDataList 派生。
例如,如果 BaseObject 实现 IDisposable,您可以将此方法添加到您的类中
,那么您只需编写一次
you could certainly pull up some of your functionality into a base class:
obviously this is just a rough sketch because i don't know all your requirements, but depending on how much implementation is shared in BaseObject you might be able to put a lot of functionality into BaseDataList (you should feel free to choose a better name). then all your other lists could just derive from BaseDataList.
For example if BaseObject implements IDisposable you could add this method to your class
then you would only have to write it once
您应该创建一个继承自
System.Collections.ObjectModel.Collection
的通用通用基类。You should make a common generic base class that inherits from
System.Collections.ObjectModel.Collection<T>
.您最好的选择是将扩展方法添加到您的 IEnumerable 集合中,即
Your best bet is to add extension methods to your IEnumerable collection i.e.