views:

75

answers:

2

I'm thinking about using class (singleton) with collections implemented using ConcurrentDictionary. This class will be used as a cache implementation (asp.net / wcf).

What do you think about exposing these collections explicitely from such class vs exposing just e.g. 3 methods (get,add,clear) for each of them (using safe methods from CD) ?

+3  A: 

As you're implementing a cache then I'd suggest only exposing those methods that you need to the outside world, to prevent any unexpected side-effects that result if a.n.other user fiddles with the dictionary.

Will A
Absolutely right. The thing to remember is that the use of ConcurrentDictionary is just coincidental: for all we know, the next implementation might use something else. So hiding all those implementation details behind an interface that provides just the right level of abstraction will make for much more maintainable code.
Steven Sudit
I was going to post this answer. Maybe you can post a simple example to make your point extremely clear?
ChaosPandion
+1  A: 

A cache without an expiration policy is a memory leak. Whatever policy you come up with is going to affect the Add() method. You have to wrap that method to initialize stuff that lets your expiration logic work. You can't just let the client code manipulate the collection directly.

So, yes, create a class wrapper.

Hans Passant
Actually, I'm going to pair it with Sql Dependency. Events from sql server will be clearing the cache and when the code will work on, cache will get filled again lazily ( so I won't fill up everything upon changes in db - just clearing up ).
StupidDeveloper