Fog Creek Software
Discussion Board

Loading session values - best practice?

I've got a complex status page in I'm stashing a bunch of values in the session to maintain the user-defined state of the page.

When I reload the page, I have to reload all the session values - as far as I can tell, my options are:


    //leave initial value

But I've got to do one of those blocks for each of 8-10 values. Of course I can sub the thing out, but I want to see if that's what most people are doing.


Monday, September 8, 2003

I'm a Java person, but I usually use your first method. I wrote a little method to do it for me, and it works fine.

Of course, this is such a trivial part of my software, I really never worried about it!

!El gato es muy gordo!
Monday, September 8, 2003

I usually have all the variables I need in a class, then I just save and load the object to Session. Their is probably something horribly inefficient with this method, but so far it doesn't seem to be a problem.

Monday, September 8, 2003

Your first method, as written, will not work.  It attempts to cast to a string which will throw an exception if it's anything but a string, then attempts to cast as an int which will throw an exception if it's anything but an int.  Casting doesn't perform conversion between types. 

Pedantry aside, the "is" operator might be of use to you to cut down on the verbosity:

    if (Session["key"] is int)

Personally, I'd rather not hide the exceptions.  If something wrong ends up in Session, it's because you put something wrong there.  I'd rather see and fix these errors as soon as possible. 

I like Bill's idea of storing a single state object.  The repetition of "key" and "int" in the above example are error prone because someone is bound to come along and copy & paste to add a new variable but miss changing one of these.  This method takes away the ability to check validity item by item, but as I mentioned above, I don't see why this is an issue in the first place.

Monday, September 8, 2003

Create a class with all the variables (strong typed). Serialize to XML and put the XML into the session object.
And vice versa
The XML is always there and the variables are guaranteed to be of the type you expect.

Geert-Jan Thomas
Monday, September 8, 2003

The XML class option can also be used to store the state in an XML data island on the page or in the viewstate.
Very flexible.

Geert-Jan Thomas
Monday, September 8, 2003

(a) Do XML data islands work in Netscape? 
(b) Are they xhtml compliant?

Monday, September 8, 2003

I don't know?
Is it required?

Geert-Jan Thomas
Monday, September 8, 2003

> (a) Do XML data islands work in Netscape? 
> (b) Are they xhtml compliant?

I don't think that that matters. Philo seems to be asking a server-side question.


Monday, September 8, 2003

Philo - If you have MS Application Center test, it would be interesting to know how much of a difference between the two methods posted and a third of doing nothing and just reloading the values.

Monday, September 8, 2003

So, how do you transform a class to XML, quickly, using C#?

Thank you!

Tuesday, September 9, 2003

Forget the XML -- why bother? Session can store any objects. I note you were storing the key as a string - again why? ANY object/value type can be stored in Session. If you're in a webfarm and using StateServer session or SQL-backed session then the classes you store in Session need to be marked as [Serializable].

Yes write a class to encapsulate the stuff you want in session as well as the storing retieving from session. We have a UserInfo clas which has a static Current property which deals with the retriving of the data.

Duncan Smart
Tuesday, September 9, 2003

I prefer to create a class to handle session & declare the session values as public properties.  That way, the casting is all done within the class & you can use Intellisense to make sure the values you're getting/setting are typed correctly.

I'm not sure how performant this is compared to other methods, but it's a hell of a lot easier for me to remember (txtTextbox.Text = SessionVariablesClass.Value) than it is to do all the checking as to whether or not the correct types are being processed and cast them on the fly.

Jeff MacDonald
Wednesday, September 10, 2003

*  Recent Topics

*  Fog Creek Home