![]() |
Web Development »
ASP.NET »
General
Intermediate
Create Typed Session State in ASP.NETBy Scott JuranekWrap the Session object in ASP.NET to prevent programming errors and keep code clean |
C++, Windows, .NET 1.0, ASP.NET, Visual Studio, MFC, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 8 Jun 2002 Editor: Paul Watson |
Copyright 2002 by Scott Juranek Everything else Copyright © CodeProject, 1999-2009 Web22 | Advertise on the Code Project |