Kevin Marshall's Epic Work Blog for Awesome People

Kevin the Coder is a maverick. Almost like a team of mavericks.
in

Taming Session State

In an ASP.NET, its helpful to store a *few* commonly used items in session. But then you get lazy and you add a few more here and there. You try to add constants as the session key, but its too late. You added Session[EMAIL] and another developer adds Session[EMAILADDRESS]. Its mass hysteria or at the very least, a tad bit irritating and inefficient. One way to help clean this up is to make a session manager class and store that in session. Then you can add whatever strongly typed properties you need to this class. The implemenation is similar to a singleton pattern except that there is no static member variable to hold an instance of itself, rather it is storing itself in session. Did that last line even make sense?

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;

/// <summary>
/// SessionManager provides strongly typed access to session information
/// 
</summary>
public class SessionManager
{
    private const string SESSIONMANAGER = "SessionManager";
    private int _userID;
    private string _email;

    /// 
<summary>
    /// ID of current user
    /// 
</summary>
    public int UserID
    {
        get { return _userID; }
        set { _userID = value; }
    }

    /// 
<summary>
    /// Email address of current user
    /// 
</summary>
    public string Email
    {
        get { return _email; }
        set { _email = value; }
    }

    /// 
<summary>
    /// Private constructor for singleton
    /// 
</summary>
    private SessionManager() {}

    /// 
<summary>
    /// Retrieves SessionManager object
    /// 
</summary>
    public static SessionManager GetInstance()
    {
        if (System.Web.HttpContext.Current.Session[SESSIONMANAGER] == null)
        {
            SessionManager sessionManager = new SessionManager();
            System.Web.HttpContext.Current.Session[SESSIONMANAGER] = sessionManager;
        }
        return (SessionManager)System.Web.HttpContext.Current.Session[SESSIONMANAGER];
    }

    /// 
<summary>
    /// Removes SessionManager from session
    /// 
</summary>
    public static void Abandon()
    {
        System.Web.HttpContext.Current.Session.Remove(SESSIONMANAGER);
    }
}
 
Hmmm, for some reason my nifty little code colorizer is not working. Actually, I'm color blind so maybe it is.  Oh well, you get the idea. I hope this helps you manage session a little better.  But remember kids, try to store only basic types like int, string, etc and avoid storing STA COM objects.  For the love of god, no STA COM objects. It affects performance and its just not cool.
Posted: Feb 23 2006, 07:45 PM by kmarshall | with 1 comment(s)
Filed under:

Comments

Ryan said:

Great trick.  Definitely saves a ton of pain with Session State and is really easy to code.  Thanks.

# August 5, 2008 2:17 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)