Question about custom collections in C #

I would like to know what is the best pattern when returning objects from custom collection classes. To illustrate my problem, here is an example:

I have a Customer class:

public class Customer { //properties //methods } 

Then I have a client collection class:

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

Now an alternative version of this method could be:

 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; } } 

I would like to discuss which one is the best approach? And is there any other approach better than two over two?

+4
source share
8 answers

I would suggest a third approach:

Edit: I updated this sample code to reflect the OP comments below.

 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; } } 

In most cases, a custom collection is not needed - I assume this is one of these cases. You can also add utility methods per type (in this case, the Customer type), as this helps the developer to open these methods. (This question depends more on taste - since this is a static method, you can put it in any type that you need CustomerUtility or CustomerHelper for example).

My last suggestion is to return the interface type from FindCustomers() to provide you with more flexibility in the future for changes in the implementation. Obviously, DAL.GetCustomers() would have to return some type that IList<T> implemented, but then any API method (especially at another level, such as the data level) should also return interface types.

+15
source

In my opinion, both of them are a little strange and confusing. When you extend the Collection class, you mean that your IS class is a collection, so that it contains data. I think that when you make this method static in the first case, it will make the most sense:

 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
source

If you really want to use these methods in the CustomerCollection class, I would suggest

 public static ICollection<Customer> GetAllCustomers() 

or

 public void FillWithAllCustomers() 

However, I would say that your CustomerCollection class is redundant, and users can go directly to the DAL if they want to get a collection of customer objects.

+2
source

Another way:

 public class Customercollection: Collection<Customer> { } public class Customer { public static CustomerCollection FindCustomers() { return DAL.GetCustomers(); } } 
+1
source

I would put the FindCustomers method in the DAL class or create a Finder class to store this method. Most likely, you will need more search methods later.

+1
source

The collection extension is in the first example, but you never use CustomCollection to store any clients. Instead, you return a Collection <Customer>. My suggestion:

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

Used as follows:

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

The second example is also a bit strange, because if you call FindCustomers twice, you will get each client twice in the list.

0
source

What if you do something like this:

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

Using List in your constructor allows you to use useful methods in List in your class without having to write your own implementation.

0
source

I would add this extension method to Andrew's suggestion:

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

and use it as follows:

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

Or, if you go with Andrew's advice on returning interface types,

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

All Articles