Click here to Skip to main content
15,879,348 members
Articles / Web Development / ASP.NET
Article

Create Typed Session State in ASP.NET

Rate me:
Please Sign up or sign in to vote.
4.47/5 (12 votes)
8 Jun 20021 min read 118.6K   43   10
Wrap the Session object in ASP.NET to prevent programming errors and keep code clean

Having a strongly-typed object or interface to develop against is generally much easier than dealing with loosely-typed interfaces. Many common errors can be caught at compile time rather than at run time, which hastens development time.

One of the things that has bothered me about the Session object in ASP.NET is that it is loosely typed, returning everything as the Object base type. Equally bothersome, the Session object requires you to pass in a string key to obtain the object you want. I don’t know how many times I’ve forgotten what key I was using for a piece of data and had to look through my code to find it. Maintaining a data dictionary in a separate document helped, but I would sometimes start using a new piece of data and forget to update my dictionary!

After having an application developed where I had the Session object sprinkled throughout my code I realized that I could create an object to wrap the Session object and clean up my code. It looks something like this:

C#
public class State {
	public static int CustomerId{
		get{ return (int)HttpContext.Current.Session["CustomerId"]; }
		set{ HttpContext.Current.Session["CustomerId"] = value; }
	}
}

Now code that once appeared as:

C#
DataTable tbl = GetInvoices((int)Session["CustomerId"]);

now looks like this:

C#
DataTable tbl = GetInvoices(State.CustomerId);

It takes a little bit of work to setup each session variable but once you try it I think you’ll find your code is a whole lot cleaner and bug free.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Chief Technology Officer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralGood idea but how about autogenerating Pin
Rama Krishna Vavilala10-Jun-02 12:41
Rama Krishna Vavilala10-Jun-02 12:41 
GeneralRe: Good idea but how about autogenerating Pin
Scott Juranek10-Jun-02 13:33
Scott Juranek10-Jun-02 13:33 
GeneralRe: Good idea but how about autogenerating Pin
Rama Krishna Vavilala10-Jun-02 14:00
Rama Krishna Vavilala10-Jun-02 14:00 
GeneralRe: Good idea but how about autogenerating Pin
Scott Juranek10-Jun-02 14:15
Scott Juranek10-Jun-02 14:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.