Page Inheritance and Wrapping the session
Published 3.21.2006 by ~mattg
My experience with ASP.NET is rather limited. Not that I don’t know what I’m doing, but I don’t have the “knowledge of experience” to tell me how things can be done more efficiently. The application we use at work is pretty well structured, so rarely do I make sweeping changes to its structure. However, my side project is in desperate need of some restructuring, and I have been able to to take my experience with the work application and apply it to my side project. Two structural elements which I highly recommend for any good-sized asp.net application are page inheritance and wrapping the session.
** Note: This may be old hat for some people… I don’t care, it helps me learn and some people may not know about it.
So what do you mean, wrap the session? Well, basically, you create a class (which doesn’t have to derive from any web controls or pages) that takes an HttpSessionState as it’s constructor. This class contains properties and functions to get and set all of the data you want to store in the session state.
So why do it this way? Well, for one thing, you don’t have to remember what string key you used for a particular piece of data. That key is defined once as a constant in the wrapper class. Second, and more importantly, the wrapper class allows you to get and set type-specific values into the session. So, for example, if you are storing an integer ID in the session, the get function will always return a valid integer (it may be 0 or -1 if the ID is not set, but it will always be an integer).
A simple session wrapper would look something like this:
{
public class SessionWrapper
{
#region Constructors
public SessionWrapper(System.Web.SessionState.HttpSessionState session)
{
m_session = session;
}
#endregion
#region Public Member Functions
public int CurrentID
{
get
{
if (m_session[CURRENT_ID] == null)
return -1;
return (int)m_session[CURRENT_ID];
}
set
{
m_session[CURRENT_ID] = value;
}
}
#endregion
#region Private Member Variables
private System.Web.SessionState.HttpSessionState m_session;
private const string CURRENT_ID = “CurrentID”;
#endregion
}
So all you have to do is create an instance of the SessionWrapper class and you can get or set CurrentID, always knowing you will get an integer back. With ASP.NET, the problem becomes constantly instantiating the SessionWrapper on every postback for every page.
That’s where the page inheritance portion comes in. If you create a PageBase class that derives from System.Web.UI.Page, all the pages in your project can derive from PageBase instead of System.Web.UI.Page. PageBase then allows you to override some of the events in the Page for EVERY derived page. In this example, we can use the PageBase class to instantiate a SessionWrapper class and provide it to the derived classes. A simple PageBase would look something like this:
{
public class PageBase : System.Web.UI.Page
{
protected SessionWrapper sessionWrapper
{
get
{
if m_sessionWrapper == null then
m_sessionWrapper = new SessionWrapper(base.Session);
return m_sessionWrapper;
}
}
private SessionWrapper m_sessionWrapper;
}
Now, if you setup your pages to inherit from PageBase instead of System.Web.UI.Page, you’ll have access to a SessionWrapper class in every one of them. No more keeping track of what’s in the session.
This same concept can be applied to the ViewState as well, although you have to remember that the ViewState exists for the life of that particular page only, so don’t store anything there that you can’t afford to lose if they navigate off of the page.
Like I said above, this may be old hat for many people, and if it is, well, you read too much.
Filed under .NET Development, Web Development
i’ve a good amount of experience with ASP.NET, and the PageBase idea i’ve been doing for years — but i was looking for a way to inherit the Session object, BUT - it is sealed, and not inheritable;
Your idea for the SessionWrapper was GREAT. I found an article about doing sort of a ‘VB6 Inheritance’ of it, but that is a LOT of code. Your approach is much cleaner.
congrats!
eidylon,
Thanks for the compliments, but as is typical, I just built on someone else’s implementations. The PageBase and type-specific sessionwrapper stuff I learned through a colleague. I took the idea a step further by creating a web “wizard” base using the viewstate as the main information store.