代码之家  ›  专栏  ›  技术社区  ›  Raghav

关于C中自定义集合的问题#

  •  4
  • Raghav  · 技术社区  · 16 年前

    我想知道从自定义集合类返回对象时,最好的模式是什么。为了说明我的问题,下面是一个例子:

    我有一个客户班:

    public class Customer
    {
       //properties
       //methods
    }
    

    然后我有一个客户收集类:

    public class Customercollection: Collection<Customer>
    {
    
      public Collection<Customer> FindCustomers()
       {
         //calls DAL and gets a Collection of customers
         Collection<Customer> customers = DAL.GetCustomers();
    
         return customers;
       }
    }
    

    现在,此方法的另一个版本可以是:

    public class Customercollection: Collection<Customer>
    {
    
      public Collection<Customer> FindCustomers()
       {
         //calls DAL and gets a Collection of customers
         Collection<Customer> customers = DAL.GetCustomers();
         foreach(Customer c in customers)
         this.Add(c);
         return this;
       }
    }
    

    我想讨论哪一种方法更好?还有没有比上述两种方法更好的方法呢?

    8 回复  |  直到 16 年前
        1
  •  15
  •   Andrew Hare    16 年前

    我建议采用第三种方法:

    编辑: 我已经更新了这个代码示例,以反映下面的OP注释。

    public class Customer
    {
        public static ICollection<Customer> FindCustomers()
        {
            Collection<Customer> customers = new Collection<Customer>();
    
            foreach (CustomerDTO dto in DAL.GetCustomers())
                customers.Add(new Customer(dto));  // Do what you need to to create the customer
    
            return customers;
        }
    }
    

    大多数时候不需要自定义集合——我假设这是其中之一。此外,还可以将实用程序方法添加到类型(在本例中, Customer 类型)因为这有助于开发人员发现这些方法。(这一点更像是一种品味问题——因为这是一种静态方法,所以你可以随意将其放在你想要的任何类型中 CustomerUtility CustomerHelper 例如)。

    我最后的建议是从返回接口类型 FindCustomers() 为将来实现更改提供更大的灵活性。明显地 DAL.GetCustomers() 必须返回一些实现的类型 IList<T> 同样,但是任何API方法(尤其是在数据层这样的不同层中)也应该返回接口类型。

        2
  •  3
  •   Grzenio    16 年前

    在我看来,他们两个都有点奇怪和困惑。当您扩展集合类时,您有点暗示您的类是一个集合,因此它包含数据。我认为,当您在第一种情况下使此方法静态化时,它将最有意义:

    public class Customercollection: Collection<Customer>
    {
    
      public static Collection<Customer> FindCustomers()
       {
         //calls DAL and gets a Collection of customers
         Collection<Customer> customers = DAL.GetCustomers();
    
         return customers;
       }
    }
    
        3
  •  2
  •   Matt Howells    16 年前

    如果您真的想要CustomerCollection类上的那些方法,那么我建议

    public static ICollection<Customer> GetAllCustomers()
    

    public void FillWithAllCustomers()
    

    但是,我认为您的CustomerCollection类是多余的,如果消费者想要获取一组客户对象,他们可以直接转到DAL。

        4
  •  1
  •   Dan Diplo    16 年前

    另一种方法是:

    public class Customercollection: Collection<Customer>
    {
    }
    
    public class Customer
    {
        public static CustomerCollection FindCustomers()
        {
            return DAL.GetCustomers();
        }
    }
    
        5
  •  1
  •   Eric Schneider    16 年前

    我将把findcustomers方法放在dal类中,或者创建一个finder类来保存该方法。很可能你以后会需要更多的查找方法。

        6
  •  0
  •   Mikael Sundberg    16 年前

    在第一个示例中扩展集合,但从不使用CustomerCollection存储任何客户。相反,您返回的是一个集合<客户>。我的建议是:

    public static class Customers
    {
    
      public static Collection<Customer> FindCustomers()
       {
         //calls DAL and gets a Collection of customers
         Collection<Customer> customers = DAL.GetCustomers();
    
         return customers;
       }
    }
    

    像这样使用:

    Collection<Customer> customers = Customers.FindCustomers();
    

    你的第二个例子也有点奇怪,因为如果你给findcustomers打两次电话,你会在列表中给每个客户打两次电话。

        7
  •  0
  •   Vivek    16 年前

    如果你这样做怎么办:

    public class CustomerCollection: Collection<Customer>
    {
      public CustomerCollection: : base(new List<Customer>())
       {}
    
      public static IList<Customer> FindCustomers()
      {
       //return them from DAL
      }
    }
    

    在构造函数中使用list可以让您在类中使用list中有用的方法,而无需编写自己的实现。

        8
  •  0
  •   Alexey Romanov    16 年前

    我将把这个扩展方法添加到安德鲁的建议中:

    public static Collection<T> ToCollection(this IEnumerable<T> seq) {
        return new Collection<T>(seq.ToList());
    }
    

    像这样使用:

    public static Collection<Customer> FindCustomers() { 
        return DAL.GetCustomers().Select(dto => new Customer(dto)).ToCollection();
    }
    

    或者如果你按照安德鲁的建议返回接口类型,

    public static IList<Customer> FindCustomers() { // or ICollection
        return DAL.GetCustomers().Select(dto => new Customer(dto)).ToList();
    }
    
    推荐文章