c# - Wrapping a nested collection -


i'm working on wrapper third party component (not best idea, know) purchased, grid precise. well, exposing of grid properties hasn't been hard far... real issue rows/cells collection.

the seniors/leads don't want developers going crazy in those, having sort customrowcollection of customrow objects wrapping bare minimum of actual grid's rowcollection of row objects idea... same goes cells, customrow object should have customcellcollection of customcells wrapping actual gridrow's cellcollection of cell objects.

do have advice on how should approach this? can't quite picture design. thanks

the customrowcollection wrapper around rowcollection. it's not collection @ all; it's class implements icollection<customrow> , translates each method appropriate method on underlying rowcollection. instance:

public void add(customrow r) {    _rowcollection.add(r.row); } 

the problem evident if at, say, indexer:

public customrow this[int i] {    { return new customrow(_rowcollection[i]); }    set { _rowcollection[i] = value.row; } } 

you're creating new customrow object every time customrow out of customrowcollection. can cause lot of problems. instance, fail, , shouldn't:

debug.assert(mycustomrowcollection[0] == mycustomrowcollection[0]); 

there couple of ways around this. can shadow rowcollection private dictionary<row, customrow> field. indexer this:

public customrow this[int i] {       {       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 ever creating 2 customrow objects same underlying row. it's lot of work, because have deal keeping collections in sync each other. (it's bad if grid ever deletes row rowcollection without notifying you, because you're still maintaining reference row in dictionary , nothing short of re-synching dictionary rowcollection ever going remove it.)

a simpler way override equals in customrow class, 2 customrow objects equal if row properties equal. have override gethashcode returns row.gethashcode(), let use customrow objects dictionary keys.

you have careful, if this, not ever implement properties in customrow aren't wrappers around properties in row. if ever this, you're introducing situation in might not fail, , should:

debug.assert(r1 == r2 && r1.property != r2.property); 

Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -