Type Safe Session Variables
This may be obvious to everyone else on the Internet. Heck, it’s been obvious to me for several years. But I can’t find anyone else writing about this and it makes so much sense that I’m kicking myself for not trying it earlier. So…
The Problem
We’ve all been there. We are coding away and need a session variable. So we create one using code that looks something like:
Session["myVar"] = "hold this for the future";
And then, on some other page in our system, we need to retrieve it:
string localVar = (string)(Session["myVar"]);
There are two problems with this:
- If I don’t retrieve the variable with exactly the same string, the data will never be there giving me a null pointer in localVar, and
- I have to cast to a string because Session[indexer] always returns an object type.
The first leaves my code prone to unknown bugs and the second is just plain annoying. So I got to wondering, "Is there a way we could wrap all of this up in a class and let the class proxy the Session object? Is it possible to have the Session data stored in a way that is both typed, so we don’t have to cast the data, and allows intellisense to tell us what the name of the item is?
Yep! It sure is.
The Solution
Obviously to get this to work, we are going to need a class; let’s call it SessionData
public class SessionData { }
We do not want this class to be instantiated. We can do this in one of two ways–we could make the class abstract, or we could make the constructor private. Since we only want to make the class so that it cannot be instantiated, it makes more sense to mark the constructor as private.
public class SessionData { private SessionData(){} }
So far, we have a class that does nothing. The next thing we need to do is implement a Session variable as a property. So let’s say we have a session variable named "myVar" that holds a string. We will need a variable to hold the string constant, so let’s create that.
public class SessionData { private const _myVar= "myVar"; private SessionData(){} }
Next we want to implement a property around the session variable. This is where a bit of ASP.NET magic comes in. The session variable is available off of the HttpContext object as well as off of the Page object. And the current context is available off of the static property Current off of the HttpContext class.
To get to the HttpContext class, we will add a using statement at the top of our file:
using System.Web;
And next, we provide the wrapper property.
using System.Web; public class SessionData { private const _myVar = "myVar"; private SessionData(){} static public string MyVar { get { return (string) (HttpContext.Current.Session[_myVar]); } set { HttpContext.Current.Session[_myVar] = value; } } }
The only question remaining is, how do you remove a session variable once you are done with it? With session variables that are pointing to objects, the answer is easy. Assign null to the property.
SessionData.MyVar = null;
But what if MyVar is an integer or some other primitive value? For this, we will need to make the property nullable by adding a ? after the type specifier. So to add a integer property to SessionData, it would look like this:
using System.Web; public class SessionData { private const _myVar = "myVar"; private const _myInt = "myInt"; private SessionData(){} static public string MyVar { get { return (string) (HttpContext.Current.Session[_myVar]); } set { HttpContext.Current.Session[_myVar] = value; } } static public int? MyInt { get { return (int?) (HttpContext.Current.Session[_myInt]); } set { HttpContext.Current.Session[_myInt] = value; } } }
Related Post
-
http://blog.reamped.net/ Ira
-
http://jclaes.blogspot.com Jef Claes
-
http://www.sholo.net/ Scott Holodak
-
http://donnyvblog.blogspot.com Donny V



