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 issu开发者_JS百科e 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 CustomRowCollection
is a wrapper around the RowCollection
. It's not actually a collection at all; it's a class that implements ICollection<CustomRow>
and translates each method into the appropriate method on the underlying RowCollection
. For instance:
public void Add(CustomRow r)
{
_RowCollection.Add(r.Row);
}
The problem with this is evident if you look at, say, the indexer:
public CustomRow this[int i]
{
get { return new CustomRow(_RowCollection[i]); }
set { _RowCollection[i] = value.Row; }
}
You're creating a new CustomRow
object every time you get a CustomRow
out of the CustomRowCollection
. This can cause a lot of problems. For instance, this will fail, and it really shouldn't:
Debug.Assert(myCustomRowCollection[0] == myCustomRowCollection[0]);
There are a couple of ways around this. You can shadow the RowCollection
with a private Dictionary<Row, CustomRow>
field. Your indexer would then look like this:
public CustomRow this[int i]
{
get
{
Row r = RowCollection[i];
if (!_Dictionary.ContainsKey(r))
{
_Dictionary[r] = CustomRow(r);
}
return _Dictionary[r];
}
set
{
_Dictionary[value.Row] = value;
RowCollection[i] = value.Row;
}
}
This prevents you from ever creating two CustomRow
objects with the same underlying Row
. 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 a Row
from its RowCollection
without notifying you, because you're still maintaining a reference to that Row
in your dictionary and nothing short of re-synching your dictionary with the RowCollection
is ever going to remove it.)
A simpler way is to override Equals
in the CustomRow
class, so that two CustomRow
objects are equal if their Row
properties are equal. You also have to override GetHashCode
so that it returns Row.GetHashCode()
, which will let you use CustomRow
objects as dictionary keys.
You have to be very careful, if you do this, not to ever implement any properties in CustomRow
that aren't wrappers around properties in Row
. If you ever do this, you're introducing a situation in which this might not fail, and it always should:
Debug.Assert(r1 == r2 && r1.Property != r2.Property);
精彩评论