I’m currently working on a wrapper for a third party component (not the best idea, I know) that we just purchased, a grid to be precise. Well, exposing most of the grid properties hasn’t been that hard so far… my real issue is with the rows/cells collection.
The seniors/leads don’t want the developers going crazy with everything in those, so having some sort CustomRowCollection of CustomRow objects wrapping the bare minimum of the actual grid’s RowCollection of Row objects is their idea… same goes for cells, a CustomRow object should have a CustomCellCollection of CustomCells wrapping the actual gridrow’s CellCollection of Cell objects.
Do you have any advice on how I should approach this? I can’t quite picture the design.
Thanks
The
CustomRowCollectionis a wrapper around theRowCollection. It’s not actually a collection at all; it’s a class that implementsICollection<CustomRow>and translates each method into the appropriate method on the underlyingRowCollection. For instance:The problem with this is evident if you look at, say, the indexer:
You’re creating a new
CustomRowobject every time you get aCustomRowout of theCustomRowCollection. This can cause a lot of problems. For instance, this will fail, and it really shouldn’t:There are a couple of ways around this. You can shadow the
RowCollectionwith a privateDictionary<Row, CustomRow>field. Your indexer would then look like this:This prevents you from ever creating two
CustomRowobjects with the same underlyingRow. But it’s a lot of work, because you have to deal with keeping those collections in sync with each other. (It’s especially bad if the grid ever deletes aRowfrom itsRowCollectionwithout notifying you, because you’re still maintaining a reference to thatRowin your dictionary and nothing short of re-synching your dictionary with theRowCollectionis ever going to remove it.)A simpler way is to override
Equalsin theCustomRowclass, so that twoCustomRowobjects are equal if theirRowproperties are equal. You also have to overrideGetHashCodeso that it returnsRow.GetHashCode(), which will let you useCustomRowobjects as dictionary keys.You have to be very careful, if you do this, not to ever implement any properties in
CustomRowthat aren’t wrappers around properties inRow. If you ever do this, you’re introducing a situation in which this might not fail, and it always should: