06 September 2010

IntelliSense & Type Safety for Session Variables

Keeping track of your Session variables in ASP.NET can become a bit cumbersome task if you end up creating many variables across a large Web Application. Sometimes you might make common mistakes like misspelling the session variable name or at times end up with incorrect data types.

I have found a very good solution for this problem which i use regularly in my ASP.NET applications. The solution is to use an "SessionHandler" class in your Application in which one creates properties to access the session variables. By this method we not only are able to access the Session variables through Visual Studio IntelliSense but also apply type safety to them.

Here is an quick example of this class:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/*
 *
 File Name : SessionHandler.cs
 Class Name: SessionHandler
 Summary   : This class is used to handle the session variables and keep a track of them using the
             SessionHandler class object.
             Advantage is that these variables are shown in VS Intellisense.
             And we can also implement type safety.
 *
 */
public static class SessionHandler
{
    ///////////////////////////////////////////////////////////////////////////////////////////

    private static string _UserKey = "UserKey";
    private static string _Username = "Username";  
    private static string _LoginType = "LoginType";
    
       

    ///////////////////////////////////////////////////////////////////////////////////////////

    public static string UserKey
    {
        get
        {
            object obj = HttpContext.Current.Session[SessionHandler._UserKey];

            if (obj == null)
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }
        set
        {
            HttpContext.Current.Session[SessionHandler._UserKey] = value;
        }
    }

  

    ///////////////////////////////////////////////////////////////////////////////////////////

    public static string LoginType
    {
        get
        {
            object obj = HttpContext.Current.Session[SessionHandler._LoginType];

            if (obj == null)
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }
        set
        {
            HttpContext.Current.Session[SessionHandler._LoginType] = value;
        }
    }

    ///////////////////////////////////////////////////////////////////////////////////////////

    public static string Username
    {
        get
        {
            object obj = HttpContext.Current.Session[SessionHandler._Username];

            if (obj == null)
            {
                return string.Empty;
            }
            else
            {
                return obj.ToString();
            }
        }
        set
        {
            HttpContext.Current.Session[SessionHandler._Username] = value;
        }
    }

}

Please Note: You need to add this class file to your AppCode folder in your Web Application.

No comments: