Recently I’ve been working with a data
access layer that I did not write, and thus did not know very well (To
tie this to “Arc” Development, this DAL stores ArcIMS map service
settings and user permission in a database). This led to a certain
level of enlightenment re: using strongly typed collections. Basically,
my lack of background knowledge about the class model left me confused
as to the “type” of objects which should be in various collections,
thus leading to some head scratching while debugging.
A little background so we’re all on the same page - a strongly typed
collection is basically a collection which only accepts a certain type
of object. As opposed to a generic collection, i.e. ArrayList, which
can hold any type of object. The upside of using a strongly typed
collection is two fold: first, you get intellisense in the IDE telling
you that the collection takes objects of type X. Second, the compiler
will catch any place that you ignored the intellisense. Any errors the
compiler can catch is an error you won’t have to debug later. Very good.
So why not use them everywhere? Basically, they are a pain to write
(inherit from collectionBase, implement a bunch of stuff etc etc), and
since there were few users of my APIs, and my class models are
inherently brialliant and totally obvious (I wish!), using an arraylist
was suitable. Now that I’m on the “consuming” end of someone elses API,
I see the value, and want to update the code, but how to get around all
that nasty coding? Code Generation!
Jeff turned me on to SharpTools
from Morrison Schwartz (free!). This has a typed collection builder in
it. For that alone you should get it. It also has other cool functions
like an RSS aggregator, a code library tool, a bunch of database tools
and a Google search tool. All excellent. If you don’t already have this
tool, download it now.
As soon as I added in the first strongly typed collection, the
compiler caught 3 errors where I was stuffing the wrong object into the
collection. So, next week, I’ll be converting all the rest of the
collections in that API over, and continuing my development. From here
on out, it’s strongly typed for me.
Of course all this changes at .NET 2.0 because we have generics to
do this, but that’s a story for another day (when I have a .NET 2.0
project!)
Re: Code Generation – In past projects we’ve done some code gen for
ArcObjects via VB6 add-ins, and we’ll be doing more on some upcoming
projects (likey usign CodeSmith), and I’ll post about that when the time comes. Should be fun!