Eric Wise wrote a post on
BasePages and inheritance in Asp.Net. Great post, and you should read it before reading this one, as this is an expansion of what he started.
I made a comment about how I do the same thing but also include a way to access the session. First lets take a look at the code Eric provided, with a few additions that supply access to a custom session manager:
using System;
public class BasePage : System.Web.UI.Page
{
public ApplicationSessionManager UserInstance;
private string _connectstring = "";
protected string connectstring
{
get
{
if(_connectstring == "")
{
//Get Connection string
}
return _connectstring;
}
}
public BasePage() : base()
{
base.Init += new System.EventHandler(PreInit);
}
protected void PreInit(object sender, System.EventArgs e)
{
UserInstance = ApplicationSessionManager.Instance;
}
}
Now lets look at the singleton ApplicationSessionManager class that goes with it. I do this primarily so that the use of the session object doesn't get out of control. I recently did a code review on an Asp.Net project that had over 45 distinct session variables. What a nightmare to maintain and keep track of! This happened because there was not solid implementation like below to define what the session should be and what should exist in it. Also, this ApplicationSessionManager class provides a strongly-typed way to access session information, as you can see below. No more (int)Session["MyInteger"] occurs, which saves quite a bit because you don't have to be knowledgable of the object type stored in session. Using the singleton below provides you that strongly-typed interface. Now lets take a look at it:
using System;
using System.Web;
[Serializable()]
public class ApplicationSessionManager
{
private ApplicationSessionManager()
{
}
public static ApplicationSessionManager Instance
{
get
{
HttpContext context = HttpContext.Current;
ApplicationSessionManager theSession;
if (!(context.Session["TheSession"] == null && (context.Session["TheSession"] is ApplicationSessionManager)))
{
theSession = ((ApplicationSessionManager)(context.Session["TheSession"]));
}
else
{
theSession = new ApplicationSessionManager();
context.Session["TheSession"] = theSession;
}
return theSession;
}
}
public void Abandon()
{
HttpContext.Current.Session.Abandon();
}
public void Clear()
{
HttpContext.Current.Session.Clear();
}
private string clientName;
public string ClientName
{
get
{
return clientName;
}
set
{
if (value == null)
{
throw new ArgumentNullException("ClientName", "Cannot store a null value in session.");
}
clientName = value;
}
}
}
As you can see, all you need to do is add a property for each item you want to store in session, and really the only thing stored in session is the ApplicationSessionManager class itself, in this case, under the name "TheSession". This is a very common way to approach this scenario and has been in use since the release of .Net.
And finally, a WebForm1.aspx.vb to go along with it
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace BaseWebProject
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : BasePage
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
UserInstance.ClientName = "Raymond Lewallen";
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Probably not much need for me to elaborate on what is going on here, as it is pretty striaght-forward. This type of scenario works extremely well for expanding out and adding core functionallity for all your pages. Again, see Eric's post for some elaboration on the BasePage inheritance technique.