Jul 21 2009

Managing Session Variables in ASP.NET using a Proxy

Category: David @ 14:51

Every developer who has ever worked with ASP.NET in a large solution knows how quickly Session variables can get out of control.  From the lack of strongly typed references to the potential misspellings of the variable names, the problems are always with us.  The good news is that there is a better way to handle objects in the Session.  The key is to develop a proxy class that will provide a set of strongly typed properties to store and retrieve objects in the HTTPSessionState object.

The Gang of Four defines the Proxy Structural Pattern as an object that serves to “Provide a surrogate or placeholder for another object to control access to it.”  A proxy is a fantastic way to provide abstraction in any application and will allow us to create a method for leveraging an ASP.NET Session object in a powerful way.

To begin with let’s create a static class (or NotInheritable in VB.NET) called SessionProxy.  The code will look something like this:

   1: using System;
   2: using System.Collections.Generic;
   3: using System.Linq;
   4: using System.Web;
   5:  
   6: public static class SessionProxy
   7:    {
   8:  
   9:       #region Properties
  10:  
  11:       public static int OrderID { 
  12:          get
  13:          {
  14:             return (int)HttpContext.Current.Session["OrderID"];
  15:          }
  16:  
  17:          set
  18:          {
  19:             HttpContext.Current.Session["OrderID"] = value;
  20:          }
  21:       }
  22:  
  23:       public static int ProductID 
  24:       {
  25:          get 
  26:          {
  27:             return (int)HttpContext.Current.Session["ProductID"];
  28:          }
  29:  
  30:          set
  31:          {
  32:             HttpContext.Current.Session["ProductID"] = value;
  33:          }
  34:       }
  35:  
  36:       #endregion
  37:  
  38:    }

 

You will notice in this initial code that we are typing the Session variable keys multiple times in each property definition.  Although this is a workable solution we still open ourselves up to the possibility of mis-spelling the two instances of the key.  So, let’s refactor our class a bit…

   1: public static class SessionProxy
   2:    {
   3:       #region Constants
   4:  
   5:       private const string ORDERID = "OrderID";
   6:       private const string PRODUCTID = "ProductID";
   7:  
   8:       #endregion
   9:  
  10:       #region Properties
  11:  
  12:       public static int OrderID { 
  13:          get
  14:          {
  15:             return (int)HttpContext.Current.Session[ORDERID];
  16:          }
  17:  
  18:          set
  19:          {
  20:             HttpContext.Current.Session[ORDERID] = value;
  21:          }
  22:       }
  23:  
  24:       public int ProductID 
  25:       {
  26:          get 
  27:          {
  28:             return (int)HttpContext.Current.Session[PRODUCTID];
  29:          }
  30:  
  31:          set
  32:          {
  33:             HttpContext.Current.Session[PRODUCTID] = value;
  34:          }
  35:       }
  36:  
  37:       #endregion
  38:  
  39:    }

 To utilize this in your code simply add a reference to the namespace where the SessionProxy exists and get or set the property through the class (ie. SessionProxy.ProductID = 123;).

Now we have a method for accessing our Session in a strongly typed manner and we are also able to see exactly what variable we have defined in our ASP.NET application by simply looking at the class definition of our SessionProxy.

Another advantage of using a proxy class like this is that it allows you to abstract your storage method as well.  If you want to use SQL Server Session or use a shared Session technology across multiple servers, all you need to do is change the property getter and setter logic and the rest of your application remains completely unchanged.

In Part 2 we will explore some enhancements to the SessionProxy.

Tags: , , , ,

Comments

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading