Question about return types with collections

In my BL (there will be a public API), I use ICollection as return types in my search methods, for example:

public static ICollection<Customer> FindCustomers() { Collection<Customer> customers = DAL.GetCustomers(); return customers; } 

Note the use of ICollection instead of Collection <>.

Now in my GUI, I need to return the results to Collection, for example:

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

This is because I need to use some special Collection <> methods in my returned list, which I cannot do with ICollection <>.

Is this the right use? Or should I just change the return type from Collection <> instead to ICollection <> to avoid this casting?

Secondly, I did not use IEnumerable, because it is more general than ICollection, and does not even have simple properties such as Count. And I really don't see the point in generalizing return types here. Did I miss something important?

+4
source share
6 answers

The whole point of using ICollection is to be more general and hide more information, which is good.

But if you need to convert it back, it makes no sense, and you could also return a more functional collection <> instead.

+2
source

The only reason you would like to return ICollection is because of the loose ties and inheritance of purpouses. If there is only one variant of your method (which becomes its static), you will always know the type of return (Collection), and there is no need to make it ICollection. However, if you used in a class family, there may be a virtual or abstract method that returns ICollection, then in subclass implementations you can return a collection or FunkyCollection or any object that implements the interface, so this gives you much more f lexibility, saying that you can return the collection. But for your purposes, you just need to make the return type a Collection, not an ICollection, because its a static method that will not be overridden. In addition, it causes less confusion for the user, because they do not need to quit.

+2
source

The idea of ​​returning ICollection is that you have less connection with your method. If you want to create a list later than a collection, you can do it without breaking the client code, etc.

If you use this only to get a collection (not ICollection) than IMHO, you can go to the collection knowing that you will have a less flexible method. But in any case, YAGNI .

In addition, if you are worried about design, I would suggest not to make it static, to improve the testability of your code.

0
source

Probably because Collection implements IList, which has some additional methods that ICollection does not have, IList is enough for you. What methods are you missing?

0
source

If you want your users to use Collection <> methods, you should probably return Collection <>, not ICollection.

Or maybe the use has a separate function that is internal to your assembly, which returns the type needed for your GUI:

 internal static ICollection<Customer> FindCustomers() { Collection<Customer> customers = DAL.GetCustomers(); return customers; } 
0
source

Everyone else has already given such great answers, but I just wanted to clarify that no, I do not believe that what you are doing is perfect. The reason for this is that if someone comes later and modifies / refactors / processes your BL to return some other type of ICollection (according to the API), which is actually not a Collection , you will get runtime errors in its GUI.

However, you can still return the ICollection from your BL: Then, if you really need a Collection (perhaps for one or more convenient extension methods, I guess?), Then you can create a new collection and copy the contents from one to another. This way you do not risk a run-time error. Although, ultimately, it depends (as someone already asked) about which method you are looking for in Collection ... Perhaps there is a better way to do what you want.

Good luck
-f

0
source

All Articles