Tuesday, March 17, 2009

Visualizing Linq operators: The Info Support Linq Posters


Over the last few months I have been working on a little project to make Linq ‘come to life’ in two full size posters that developers can hang on the wall of their offices. The Linq posters demonstrates the usage of all Linq operators using visual sequences of female and male figures. Since this week the final result is ready and can be ordered in Print / Downloaded from our website http://www.infosupport.com/linq free of charge!
poster_staand poster_liggend
So why create a Linq poster?

Sunday, March 15, 2009

CacheDictionary for .Net 3.5, using ReaderWriterLockSlim ?

In my previous post I described how to create a thread safe data cache using PFX. PFX however is scheduled to be released as a part of the .Net framework 4.0 which means we will have to wait a while before we can use this in real world applications. That’s why I created an implementation using the generic Dictionary combined with a ReaderWriterLockSlim which are both available in .Net 3.5. today.
   1: public class CacheDictionary
   2: {
   3:     ReaderWriterLockSlim _cacheLock = new ReaderWriterLockSlim();
   4:     Dictionary> _cacheItemDictionary = new Dictionary>();
   5:  
   6:     public TValue Fetch(TKey key, Func producer)
   7:     {
   8:         LazyInit cacheItem;
   9:         bool found;
  10:  
  11:         _cacheLock.EnterReadLock();
  12:         try
  13:         {
  14:             found = _cacheItemDictionary.TryGetValue(key, out cacheItem);
  15:         }
  16:         finally
  17:         {
  18:             _cacheLock.ExitReadLock();
  19:         }
  20:  
  21:         if (!found)
  22:         {
  23:             _cacheLock.EnterWriteLock();
  24:             try
  25:             {
  26:                 if (!_cacheItemDictionary.TryGetValue(key, out cacheItem))
  27:                 {
  28:                     cacheItem = new LazyInit(producer);
  29:                     _cacheItemDictionary.Add(key, cacheItem);
  30:                 }
  31:             }
  32:             finally
  33:             {
  34:                 _cacheLock.ExitWriteLock();
  35:             }
  36:         }
  37:  
  38:         return cacheItem.Value;
  39:     }
  40: }