继承列表实施集合是一个坏主意吗?

发布于 2024-09-24 02:39:08 字数 1120 浏览 2 评论 0原文

我曾经读过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

强者自强 2024-10-01 02:39:08

我不会继承 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 no virtual methods to override). I would either use List<T> (or the more abstract IList<T>), or to introduce polymorphism Collection<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 any IList<T> (or IEnumerable<T>), not just List<T> and subclasses.

是伱的 2024-10-01 02:39:08

通过子类化 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 an Employees as List<Employee> doesn't inherit from Employees. 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-class List<Employee>.

Personally, I'd try and use IList<T> where ever possible and don't create subclasses unless they have a reason to exist.

为你拒绝所有暧昧 2024-10-01 02:39:08

一个简单的经验法则是从组合开始(例如将员工包裹在通用集合中)而不是继承。从基于继承的设计开始,你就会陷入困境。组合更加灵活和可修改。

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.

[旋木] 2024-10-01 02:39:08

我个人会将列表存储为容器对象的成员,并在必要时提供列表本身的 get 访问器。

class MyCollection<T> 
{
    List<T> _table;
    public List<T> Table
    {
        get
        {
            return _table;
        }
    }
    // ... other access/utility functions common for all tables 
    //     (CRUD for example)
}

class Employees : MyCollection<Employee>
{
    // ... yet more methods 
}

我必须承认,我这样做是为了为自己提供以下方法:

T FindById(int id);
void Add(T entity);
void Remove(T entity);

在基类上以及:

T[] GetEmployeesBy(your_filter_here);

我(仍然)使用.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.

class MyCollection<T> 
{
    List<T> _table;
    public List<T> Table
    {
        get
        {
            return _table;
        }
    }
    // ... other access/utility functions common for all tables 
    //     (CRUD for example)
}

class Employees : MyCollection<Employee>
{
    // ... yet more methods 
}

I must admit that I did that to provide myself with methods like:

T FindById(int id);
void Add(T entity);
void Remove(T entity);

on base class and:

T[] GetEmployeesBy(your_filter_here);

I (still) use .net 2.0 so I have no linq - maybe that's the reason for it here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文