Collection of custom entities

  • Thread starter Thread starter GrumpyDev
  • Start date Start date
If your "entities" are objects, just create an ArrayList and put them in
there..

ArrayList entities = new ArrayList();
entites.Add(my_entity);
 
My entities are objects. And I think ArrayList is very
good and simple solution. My concern is regarding type-
safety. As I understand Microsoft encourages developer to
use strong-typed classes. It also eliminates late binding
issue. To solve my problem, I wanted to create a custom
collection (inherited form CollectionBase, for example)
for each business entity. I don't know if I'm doing a
right thing.
 
In general, a strongly-typed class is the way to go. The
only things you really need to be aware of are the
following:
The arraylist has less overhead, however it is much less
flexible. It can also be used as a datasource. But, it
can be referenced by index only.

The CollectionBase is flexible, can be used as a
datasource and can be accessed by Index. It is very fast
when accessing by Index. The only major drawback is that
you can't access items by Key. To do this you can do what
I do which is to create an Item(Key as string) method
which can loop through the collection looking for
your "Key" value in the objects. You can also extend it
to return the collection in a sorted order.

The DictionaryBase is the other major option. This is a
name/value collection and the items can only be accessed
by key. The collection cannot be used as a datasource and
you must use a for..each loop to look through everything.
In general, if you don't know the key then this is a bad
type of collection to use. Also, the order the data is
returned is not necessarily the order they were added in.

The choice is pretty wide open, but I'm a big fan of the
collectionbase and extending it. However the choice will
depend on the type of project and the tradeoffs you want
to make.

Jeff Levinson

Author of "Building Client/Server Applications with
VB.NET: An Example Driven Approach"
 
Back
Top