继承列表实施集合是一个坏主意吗?
我曾经读过 Imaar Spaanjars 写的一篇关于如何构建 3 层应用程序的文章。 (http://imar. spaanjaars.com/416/building-layered-web-applications-with-microsoft-aspnet-20-part-1),它已经成为我编码的基础一段时间了。
因此,我像他一样通过继承 List
来实现集合。因此,如果我有一个名为 Employee 的类,为了实现一个集合,我还将有一个名为 Employee 的类,如下所示。
class Employee
{
int EmpID {get;set;}
string EmpName {get;set;}
}
class Employees : List<Employee>
{
public Employees(){}
}
我从来没有真正质疑过这一点,因为它对我有用。但现在我开始尝试一些事情,我不确定这是否是正确的方法。
例如,如果我想从Employees 获取子集,例如
Employees newEmployees = (Employees) AllEmployees.FindAll(emp => emp.JoiningDate > DateTime.Now);
This throws a System.InvalidCastException 。但是,如果我使用以下内容,则没有问题。
List<Employee> newEmployees = AllEmployees.FindAll(emp => emp.JoiningDate > DateTime.Now);
那么如何实现Employees,这样我就不必在DAL 或BLL 中显式使用List
了?或者也许我该如何摆脱 InvalidCastexception?
I once read an article by Imaar Spaanjars on how to build 3 tier applications. (http://imar.spaanjaars.com/416/building-layered-web-applications-with-microsoft-aspnet-20-part-1) which has formed the basis of my coding for a while now.
Thus I implement collections as he has done, by inheriting a List<T>
. So if I have a class named Employee,to implement a collection I will also have a class Employees as below.
class Employee
{
int EmpID {get;set;}
string EmpName {get;set;}
}
class Employees : List<Employee>
{
public Employees(){}
}
I never really questioned this as it did the work for me. But now that I started trying out a few things I am not sure if this is the correct approach.
e.g. if I want to get a subset from Employees, such as
Employees newEmployees = (Employees) AllEmployees.FindAll(emp => emp.JoiningDate > DateTime.Now);
This throws a System.InvalidCastException . However, if I use the following then there is no Issue.
List<Employee> newEmployees = AllEmployees.FindAll(emp => emp.JoiningDate > DateTime.Now);
So how do I implement Employees so that I dont have to explicitly use List<Employee>
in my DAL or BLL? Or maybe how do I get rid of the InvalidCastexception?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不会继承
List
- 它引入了类似的问题,并且没有真正的帮助(因为没有virtual
方法可以覆盖)。我要么使用List
(或更抽象的IList
),要么引入多态性Collection
有虚拟方法。作为注释;对于像
FindAll
这样的东西,您还可能会找到有用的对应项 LINQ 选项(例如.Where()
);最值得注意的是,它们适用于任何IList
(或IEnumerable
),而不仅仅是List。
和子类。I wouldn't inherit from
List<T>
- it introduces issues like these, and doesn't really help (since there are novirtual
methods to override). I would either useList<T>
(or the more abstractIList<T>
), or to introduce polymorphismCollection<T>
has virtual methods.As a note; re things like
FindAll
, you may also find the LINQ options (like.Where()
) useful counterparts; most notably, they will work for anyIList<T>
(orIEnumerable<T>
), not justList<T>
and subclasses.通过子类化
List
可以获得什么好处?如果它没有添加任何内容,那么它就是不必要的代码膨胀。如果您没有显示方法或通过此处未显示的构造函数强制执行契约,那么这可能是子类化的正当理由。在解决转换问题方面,您无法从
List
向下转换为Employees
,因为List
不会继承自Employees
。如果您需要返回具有您的条件的员工,那么您最好封装该调用并将返回的列表项插入到您自己的员工对象中。这对我来说似乎是浪费时间,除非像我上面所说的那样,您有充分的理由对List
进行子类化。就我个人而言,我会尽可能尝试使用
IList
并且不会创建子类,除非它们有存在的理由。What benefit are you getting by sub-classing
List<Employee>
? If it adds nothing then it's unnecessary code-bloat. If you're not showing methods or are enforcing contracts through a constructor that you haven't shown here then there that might be a valid reason for sub-classing.In terms of resolving your casting problem you can't downcast from a
List<Employee>
to anEmployees
asList<Employee>
doesn't inherit fromEmployees
. If you need to return an Employees with your criteria then you're best to encapsulate the call and insert the returned list items into your own Employees object. This seems like a waste of time to me unless, like I said above, you've got a good reason to sub-classList<Employee>
.Personally, I'd try and use
IList<T>
where ever possible and don't create subclasses unless they have a reason to exist.一个简单的经验法则是从组合开始(例如将员工包裹在通用集合中)而不是继承。从基于继承的设计开始,你就会陷入困境。组合更加灵活和可修改。
A simple rule of thumb is to start with COMPOSITION (e.g. wrap Employees around a generic collection ) and NOT INHERITANCE. Starting off with inheritance based design is painting yourself into a corner. Composition is more flexible and modifiable.
我个人会将列表存储为容器对象的成员,并在必要时提供列表本身的 get 访问器。
我必须承认,我这样做是为了为自己提供以下方法:
在基类上以及:
我(仍然)使用.net 2.0,所以我没有 linq - 也许这就是这里的原因。
I would personally store the list as a member of a container object, and if necesery provide get accessor to the list itself.
I must admit that I did that to provide myself with methods like:
on base class and:
I (still) use .net 2.0 so I have no linq - maybe that's the reason for it here.