Click here to Skip to main content
Licence 
First Posted 8 Jun 2002
Views 98,733
Bookmarked 43 times

Create Typed Session State in ASP.NET

By | 8 Jun 2002 | Article
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:

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:

DataTable tbl = GetInvoices((int)Session[“CustomerId”]);

now looks like this:

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

About the Author

Scott Juranek

Chief Technology Officer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralhttpContext not declared PinsussAnonymous13:55 14 Mar '04  
GeneralRe: httpContext not declared Pinmemberpmpjr16:49 16 Aug '05  
Generalclean the data in session PinmemberCZIU10:50 11 Mar '03  
GeneralRe: clean the data in session PinmemberZackJones15:23 30 Sep '03  
GeneralGood idea but how about autogenerating PinmemberRama Krishna12:41 10 Jun '02  
GeneralRe: Good idea but how about autogenerating PinmemberScott Juranek13:33 10 Jun '02  
What would you generate this class from? You could maintain a seperate XML file but many of the things I typically put in session require a very simple get/put so the code generation wouldn't buy you much. Often times my things in the 'State' class actually come from the Cache so the code generator would require some extra smarts that way also.
 
Thanks for the suggestion!
GeneralRe: Good idea but how about autogenerating PinmemberRama Krishna14:00 10 Jun '02  
GeneralRe: Good idea but how about autogenerating PinmemberScott Juranek14:15 10 Jun '02  
GeneralAlternative Idea PinmemberAnonymous5:43 10 Jun '02  
GeneralRe: Alternative Idea PinmemberGünther Makulik12:08 10 Jun '02  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 9 Jun 2002
Article Copyright 2002 by Scott Juranek
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid