Alex Rivera | Logout

List<BusinessObject> or BusinessObjectCollection?

Asked 2008-08-22T03:25:09.487
47

Prior to C# generics, everyone would code collections for their business objects by creating a collection base that implemented IEnumerable

IE:

public class CollectionBase : IEnumerable

and then would derive their Business Object collections from that.

public class BusinessObjectCollection : CollectionBase

Now with the generic list class, does anyone just use that instead? I've found that I use a compromise of the two techniques:

public class BusinessObjectCollection : List<BusinessObject>

I do this because I like to have strongly typed names instead of just passing Lists around.

What is your approach?

Edit
Report

3 Answers

1

I use generic lists for almost all scenarios. The only time that I would consider using a derived collection anymore is if I add collection specific members. However, the advent of LINQ has lessened the need for even that.

answered 2008-08-22T05:02:08.037
0

I tend to do it with my own collection if I want to shield the access to the actual list. When you are writing business objects, chance is that you need a hook to know if your object is being added/removed, in such sense I think BOCollection is better idea. Of coz if that is not required, List is more lightweight. Also you might want to check using IList to provide additional abstraction interface if you need some kind of proxying (e.g. a fake collection triggers lazy load from database)

But... why not consider Castle ActiveRecord or any other mature ORM framework? :)

answered 2008-09-18T16:11:12.743
0

I would do this:

using BusinessObjectCollection = List<BusinessObject>;

This just creates an alias rather than a completely new type. I prefer it to using List<BusinessObject> directly because it leaves me free to change the underlying structure of the collection at some point in the future without changing code that uses it (as long as I provide the same properties and methods).

answered 2008-10-10T16:07:09.983

Your Answer