There are a few blogs with very interesting ways to manage objects stored in the session. Some of these approaches offer very cool ways to maintain static typing yet still only store what you need to in the session. The way that we will approach this issue is not very original, nor is it clever. But it is simple, and has its benefits.
Ad Hoc - the old way
If you need to store the user's city and state in the session so that they don't have to enter it again on some other page, you would do something similar to the following:
Each item that needed to be stored in the session got thrown into the session, with a key to indicate which object is where. The benefit to this approach is, the least amount of data is being stored for each item. It is fast to code and easy... at first. Once your project explodes in size, and you find yourself trying to keep track of all these keys it can become rather unwieldy.
Shoebox - the simple way
A shoebox is simply a class with a collection of properties that you'll need at one time or another. I call it the shoebox method, because you can put everything you store in there, and only worry about retrieving the one "shoebox" back. The implementation of the class is so trivial I decided not to include it, but here it is in use:
If the shoebox comes back null, you'll know the session has died. This prevents you from having to preform that check on each item you're trying to retrieve from the session.
Conclusion
There is nothing tricky or quirky about this "shoebox" method. Chances are, your time as a developer is better spent in the higher-level business logic realm than worrying about the underlying mechanisms for storing and retrieving objects in the session. To that end, this way will get you the results you need without the side effects of the ad hoc method, or the overhead of a more involved session wrapper class.
Programming