purpose of life is joy


How to : Create strongly typed generic session helper in C#?

Writing session code in ASP.NET is easy. but to organize and maintain is very difficult. To overcome that in one of our project, we have decided to created a generic Session Handler. This will replace the session keyword across the project. instead, this static session helper allow us to create session across the different pages by way of single static helper class. Here is the generic class for you.

using System; using System.Data; using System.Configuration; using System.Web; using System.Web.UI; using System.Collections.Generic; namespace StateHelper { public enum State { Application, Session, Cache, ViewState } # region Static Methods /// <summary> /// Generic session helper - to store,retrieve and remove the session objects to the HTTP cache. /// </summary> public static class GenericSessionHelper<T> { static StateBag ViewState = new StateBag(); /// <summary> /// returns generic session object from HTTP cache by the session key /// </summary> /// <param name="sessionKey">Session Key name</param> /// <returns>Generic Object</returns> public static T Get(string sessionKey, State state) { //object keyValue =HttpContext.Current.Session[sessionKey]; //if (keyValue != null) // return (T)keyValue; //else // return default(T); object keyValue=null; switch (state) { case State.Application: if (HttpContext.Current.Application[sessionKey]!= null) keyValue=(T)HttpContext.Current.Application[sessionKey]; break; case State.Session: if (HttpContext.Current.Session[sessionKey] !=null) keyValue= (T)HttpContext.Current.Session[sessionKey]; break; case State.Cache: if (HttpContext.Current.Cache[sessionKey] !=null) keyValue=(T)HttpContext.Current.Cache[sessionKey]; break; case State.ViewState: if (ViewState[sessionKey] != null) keyValue= (T)ViewState[sessionKey]; break; } if (keyValue != null) return (T)keyValue; else return default(T); } /// <summary> /// set the session object to HTTP cache with given session key /// </summary> /// <param name="sessionKey">Session Key name</param> /// <param name="sessionValue">Session Key Value</param> public static void Set<T>(string sessionKey, T sessionValue, State state) { switch (state) { case State.Application: HttpContext.Current.Application.Add(sessionKey, sessionValue); break; case State.Session: HttpContext.Current.Session.Add(sessionKey, sessionValue); break; case State.Cache: HttpContext.Current.Cache.Insert(sessionKey, sessionValue); break; case State.ViewState: ViewState.Add(sessionKey, sessionValue); break; } } /// <summary> /// remove the session object from the HTTP cache based on session key /// </summary> /// <param name="sessionKey">Key Name</param> public static void Remove(string sessionKey, State state) { ///HttpContext.Current.Session.Remove(sessionKey); switch (state) { case State.Application: HttpContext.Current.Application.Remove(sessionKey); break; case State.Session: HttpContext.Current.Session.Remove(sessionKey); break; case State.Cache: HttpContext.Current.Cache.Remove(sessionKey); break; case State.ViewState: ViewState.Remove(sessionKey); break; } } } #endregion }
blog comments powered by Disqus
Protected by Copyscape Web Plagiarism Check