Web applications generally start simple with a few pages, transitions between pages and a few things to be communicated between pages. The data needed between pages are either passed on as query string or stored and retrieved from session.
Slowly, more and more pages start sprouting in the application. Each of these start needing some data captured or processed from the user in some page on the way to the current page. They also want to store some of their own information.
The application is functional, we can hit the web site and start browsing and shopping for articles on it. As time passes, we start discovering that a few knots are being formed over the items in the session. A few chinks in the armor are found when, objects stored in the session start getting removed, replaced or added without intention.
These spurious errors happen due to pollution of key names in the session.
There is a good chance that register user page would have saved the user registering time by:
Session["StartDate"] = DateTime.Today;Similarly, assume the user is on a trial period for a subscription from tomorrow, the subscribe page might have:
Session["StartDate"] = DateTime.Today.AddDays(1);So, without knowledge we have polluted the key space and tied our self with our own shoe laces. Now!, you might say, that's a school boy mistake.
You could avoid this at first by having keys like "User_Start_Date" or even have a constant string defined in a global constants class available in all pages. This way all keys are visible in one file and it is less likely to end up with same key names.
So, you would now use it like:
public class SessionKeys
{
public const string UserStartDate = "UserStartDate";
public const string TrialStartDate = "TrialStartDate";
}
Session[SessionKeys.TrialStartDate] = DateTime.Today.AddDays(1);
But, there is still enough room for human mistakes to creep in. Further more, when I am done with creation of trial subscription for the user, I have to make sure of clearing of all information I created in the process but keep other information still in session.
It seems like we are missing an abstraction here. We will look at that in the next part.
