inheritance - Inheriting/Encapsulating a concurrent collection c# -


i'm creating custom collection encapsulates concurrentdictionary. found lot of information on encapsulating/inheriting generic collection nothing specific concurrent collections. here code snippet of base case, followed general questions.

class itemcollection {     private concurrentdictionary<string, item> collection;              public itemcollection() { collection = new concurrentdictionary<string, item>(); }      public bool tryadd(string webid, string name) { return collection.tryadd(webid, new item(name)); }     public bool tryadd(string webid, item item) { return collection.tryadd(webid, item); }     public bool tryupdate(keyvaluepair<string, item> entry, data data)      {         item newitem = entry.value;         newitem.adddata(data);                     return collection.tryupdate(entry.key, newitem, entry.value);     } } 
  • is encapsulating concurrent collections in manner acceptable, or moving realm of creating own thread-safe collection generic collection?
  • is custom collection thread-safe?
  • is inheriting concurrent collection ever acceptable? class itemcollection : concurrentdictionary<string, item> , if guidelines, similar this inheriting non-concurrent collections.
  • how implement forwarding methods methods select? tried series of variations following can't work:

public ienumerable<tresult> select<itemcollection, tresult>(this itemcollection source, func<keyvaluepair<string, item>, tresult> selector) { return collection.select(selector); }

if inherit concurrentdictionary results in implementation enter image description here

the question bordering on "too broad". said, basic answers can provided. more detail, have narrow question(s) down further, dealing smaller, more specific issues @ time, , preferably presenting questions individually.

in meantime:

is encapsulating concurrent collections in manner acceptable, or moving realm of creating own thread-safe collection generic collection?

"acceptable" broad concept, but…yes, there's nothing fundamentally wrong approach.

is custom collection thread-safe?

"thread safe" vague term. in general, i'd "yes". class doesn't maintain of own state @ all, instead delegating state class is thread-safe. usual concerns of thread-safety, class state should fine.

note tryupdate() method modify passed-in object, not done in thread-safe way. that's more thread safety of object, not of collection.

is inheriting concurrent collection ever acceptable? class itemcollection : concurrentdictionary , if guidelines, similar inheriting non-concurrent collections.

it's "acceptable" inherit non-sealed class. whether it's useful different matter. inheritance useful when inherited class has useful protected and/or virtual members derived class can use extend or otherwise modify behavior of inherited class.

otherwise, extension methods better approach.

in case, not expect guidance inheritance of collections vary if @ depending on whether collection thread-safe or not.

how implement forwarding methods methods select? tried series of variations following can't work:

"methods select()" implemented extension methods, , rely on object implementing ienumerable<t> (for such methods, ienumerable enough). if want custom collection able use methods, you'll have implement appropriate interface extension method(s) want used class.

naturally, in implementation of interface, can delegate actual operation encapsulated collection.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -