Or you could think of this as session objects. Either way the end result is the same. Everyone uses session variables at some point in their career. What I am doing is offering what I feel is a better way to deal with session variables.
When I have used session variables in the past I had two main issues; having to remember names and converting them when they were other than string. Not to mention using Session["SomeName"] was just too much typing without intellisense. Some people have wrapped their session variables with getters and setters. That is a nice idea, but still a bit too much work for what I want to do. After all I want my code to work for me, not just work. What if we save a class as a session object, and have the class save itself seamlessly as a session variable. This works great because you access the class thus giving you intellisense, strongly typed variables and you don't need to worry about checking the session since it is all in the class.
To demonstrate this lets create a class that we will use for session information. Our example class will want to store a name, an age, and a birthday. First, I will write it as a normal every day class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
///
<summary>
/// Simple class
to track some
/// user
information.
///
</summary>
public class NormalUserInfo
{
#region Variables
private string myName;
private int myAge;
private DateTime myBirthday;
#endregion
#region Constructors
public NormalUserInfo()
{
Name = "";
Age = 0;
Birthday = DateTime.Now;
}
#endregion//Constructors
#region Public Methods
public string Name
{
get { return this.myName; }
set { this.myName = value;
}
}
public int Age
{
get { return this.myAge; }
set { this.myAge = value; }
}
public DateTime Birthday
{
get { return this.myBirthday; }
set { this.myBirthday = value;
}
}
#endregion//Public Methods
#region Private Methods
#endregion//Private Methods
}
Now we have a normal class that we would use in a normal website. We have private variables, public getters and setters to access the variables, and a constructor. Lets turn this class into something we can use as a session object.
First we want to decorate it with the Serializable attribute. While this does not impact the use of this as a session object it allows us to store this class in other ways in the future if we desire it.
///
<summary>
/// Simple class
to track some
/// user
information.
///
</summary>
[Serializable]
public class NormalUserInfo
{
Next we need to add a couple of private methods for maintenance and a string constant. One is to save our object to the session; we will call this method often. The other is to check for our object already existing and initialize our object to the existing one. The string constant will be our session variable name.

Paul GisbornePosted Oct 15, 2016, 7:45 AM
Wow! Since I started sending objects to session variables my code has been much simpler to manage. Now I plan to incorporate this technique going forwards which will make thinking about State management even more straightforward.
Mahesh ChandPosted Sep 22, 2010, 8:09 AM
Thank you for sharing. I have seen this problem over and over where too many session variables are used and they are everywhere. This helps manage them and keep the code cleaner. Good work!