Posted By


nivlam on 02/07/12

Tagged


Statistics


Viewed 852 times
Favorited by 0 user(s)

Session Dictionary


/ Published in: C#
Save to your folder(s)

Session dictionary that wraps ASP.NET session so that it looks like a IDictionary


Copy this code and paste it in your HTML
  1. public class SessionDictionary : IDictionary<string,object>, IDictionary
  2. {
  3. readonly HttpSessionState sessionState;
  4. readonly CollectionAdapter<string> keysAdapter;
  5. readonly CollectionAdapter<object> valuesAdapter;
  6.  
  7. public SessionDictionary(HttpSessionState sessionState)
  8. {
  9. this.sessionState = sessionState;
  10. keysAdapter = new CollectionAdapter<string>(sessionState.Keys);
  11. valuesAdapter = new CollectionAdapter<object>(sessionState);
  12. }
  13.  
  14. public bool ContainsKey(string key)
  15. {
  16. return keysAdapter.Contains(key);
  17. }
  18.  
  19. public void Add(string name, object value)
  20. {
  21. sessionState.Add(name, value);
  22. }
  23.  
  24. public bool Remove(string name)
  25. {
  26. if (!ContainsKey(name)) return false;
  27. sessionState.Remove(name);
  28. return true;
  29. }
  30.  
  31. public bool TryGetValue(string key, out object value)
  32. {
  33. if (ContainsKey(key))
  34. {
  35. value = this[key];
  36. return true;
  37. }
  38.  
  39. value = null;
  40. return false;
  41. }
  42.  
  43. public void Add(KeyValuePair<string, object> item)
  44. {
  45. Add(item.Key, item.Value);
  46. }
  47.  
  48. public bool Contains(object key)
  49. {
  50. return ContainsKey((string) key);
  51. }
  52.  
  53. public void Add(object key, object value)
  54. {
  55. Add((string)key, value);
  56. }
  57.  
  58. public void Clear()
  59. {
  60. sessionState.Clear();
  61. }
  62.  
  63. IDictionaryEnumerator IDictionary.GetEnumerator()
  64. {
  65. return new DictionaryEnumerator<string, object>(KeyValueEnumerable().GetEnumerator());
  66. }
  67.  
  68. public void Remove(object key)
  69. {
  70. Remove((string) key);
  71. }
  72.  
  73. object IDictionary.this[object key]
  74. {
  75. get { return this[(string) key]; }
  76. set { this[(string) key] = value; }
  77. }
  78.  
  79. public bool Contains(KeyValuePair<string, object> item)
  80. {
  81. if (!ContainsKey(item.Key)) return false;
  82. return Equals(this[item.Key], item.Value);
  83. }
  84.  
  85. public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex)
  86. {
  87. CopyTo((Array)array, arrayIndex);
  88. }
  89.  
  90. public bool Remove(KeyValuePair<string, object> item)
  91. {
  92. if (!Contains(item)) return false;
  93. return Remove(item.Key);
  94. }
  95.  
  96. IEnumerator<KeyValuePair<string, object>> IEnumerable<KeyValuePair<string, object>>.GetEnumerator()
  97. {
  98. return KeyValueEnumerable().GetEnumerator();
  99. }
  100.  
  101. IEnumerable<KeyValuePair<string, object>> KeyValueEnumerable()
  102. {
  103. return Keys.Select(key => new KeyValuePair<string, object>(key, this[key]));
  104. }
  105.  
  106. public IEnumerator GetEnumerator()
  107. {
  108. return sessionState.GetEnumerator();
  109. }
  110.  
  111. public object this[string name]
  112. {
  113. get { return sessionState[name]; }
  114. set { sessionState[name] = value; }
  115. }
  116.  
  117. public void CopyTo(Array array, int index)
  118. {
  119. sessionState.CopyTo(array, index);
  120. }
  121.  
  122. public int Count
  123. {
  124. get { return sessionState.Count; }
  125. }
  126.  
  127. public object SyncRoot
  128. {
  129. get { return sessionState.SyncRoot; }
  130. }
  131.  
  132. public bool IsSynchronized
  133. {
  134. get { return sessionState.IsSynchronized; }
  135. }
  136.  
  137. public ICollection<string> Keys
  138. {
  139. get { return keysAdapter; }
  140. }
  141.  
  142. ICollection IDictionary.Values
  143. {
  144. get { return valuesAdapter; }
  145. }
  146.  
  147. ICollection IDictionary.Keys
  148. {
  149. get { return keysAdapter; }
  150. }
  151.  
  152. public ICollection<object> Values
  153. {
  154. get { return valuesAdapter; }
  155. }
  156.  
  157. public bool IsReadOnly
  158. {
  159. get { return sessionState.IsReadOnly; }
  160. }
  161.  
  162. public bool IsFixedSize
  163. {
  164. get { return false; }
  165. }
  166. }
  167.  
  168. public class CollectionAdapter<T> : ICollection<T>, ICollection
  169. {
  170. readonly ICollection collection;
  171.  
  172. public void CopyTo(Array array, int index)
  173. {
  174. collection.CopyTo(array, index);
  175. }
  176.  
  177. public object SyncRoot
  178. {
  179. get { return collection.SyncRoot; }
  180. }
  181.  
  182. public bool IsSynchronized
  183. {
  184. get { return collection.IsSynchronized; }
  185. }
  186.  
  187. public CollectionAdapter(ICollection collection)
  188. {
  189. this.collection = collection;
  190. }
  191.  
  192. IEnumerator<T> IEnumerable<T>.GetEnumerator()
  193. {
  194. return collection.Cast<T>().GetEnumerator();
  195. }
  196.  
  197. public IEnumerator GetEnumerator()
  198. {
  199. return collection.GetEnumerator();
  200. }
  201.  
  202. public void Add(T item)
  203. {
  204. throw new NotSupportedException();
  205. }
  206.  
  207. public void Clear()
  208. {
  209. throw new NotSupportedException();
  210. }
  211.  
  212. public bool Contains(T item)
  213. {
  214. return collection.Cast<T>().Any(x => Equals(x, item));
  215. }
  216.  
  217. public void CopyTo(T[] array, int arrayIndex)
  218. {
  219. collection.CopyTo(array, arrayIndex);
  220. }
  221.  
  222. public bool Remove(T item)
  223. {
  224. throw new NotSupportedException();
  225. }
  226.  
  227. public int Count
  228. {
  229. get { return collection.Count; }
  230. }
  231.  
  232. public bool IsReadOnly
  233. {
  234. get { return true; }
  235. }
  236. }
  237.  
  238. public class DictionaryEnumerator<TKey,TValue> : IDictionaryEnumerator, IDisposable
  239. {
  240. readonly IEnumerator<KeyValuePair<TKey, TValue>> enumerator;
  241.  
  242. public DictionaryEnumerator(IEnumerator<KeyValuePair<TKey,TValue>> enumerator)
  243. {
  244. this.enumerator = enumerator;
  245. }
  246.  
  247. public void Dispose()
  248. {
  249. enumerator.Dispose();
  250. }
  251.  
  252. public bool MoveNext()
  253. {
  254. return enumerator.MoveNext();
  255. }
  256.  
  257. public void Reset()
  258. {
  259. enumerator.Reset();
  260. }
  261.  
  262. public object Current
  263. {
  264. get { return enumerator.Current; }
  265. }
  266.  
  267. public object Key
  268. {
  269. get { return enumerator.Current.Key; }
  270. }
  271.  
  272. public object Value
  273. {
  274. get { return enumerator.Current.Value; }
  275. }
  276.  
  277. public DictionaryEntry Entry
  278. {
  279. get { return new DictionaryEntry(Key, Value); }
  280. }
  281. }

URL: http://blog.jason.smale.org/2010/08/dictionary-adapter-for-httpsessionstate_15.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.