Facebook
From Morose Prairie Dog, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 279
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.SessionState;
  6.  
  7. namespace PostawNaMilionAzure.Utilties
  8. {
  9.     public class SessionManager : ISessionManager
  10.     {
  11.         private HttpSessionState _session;
  12.  
  13.         public SessionManager()
  14.         {
  15.             _session = HttpContext.Current.Session;
  16.         }
  17.  
  18.         public void Set<T>(string name, T value)
  19.         {
  20.             _session[name] = value;
  21.         }
  22.  
  23.  
  24.         public T Get<T>(string key)
  25.         {
  26.             return (T)_session[key];
  27.         }
  28.  
  29.         public T TryGet<T>(string key)
  30.         {
  31.             try
  32.             {
  33.                 return (T)_session[key];
  34.             }
  35.             catch (NullReferenceException)
  36.             {
  37.                 return default(T);
  38.             }
  39.         }
  40.  
  41.         public T Get<T>(string key, Func<T> createDefault)
  42.         {
  43.             T retval;
  44.  
  45.             if (_session[key] != null && _session[key].GetType() == typeof(T))
  46.             {
  47.                 retval = (T)_session[key];
  48.             }
  49.             else
  50.             {
  51.                 retval = createDefault();
  52.                 _session[key] = retval;
  53.             }
  54.  
  55.             return retval;
  56.         }
  57.  
  58.         public void Abandon()
  59.         {
  60.             _session.Abandon();
  61.         }
  62.     }
  63. }