Base Pages and Interfaces for ASP.NET Pages
- by geekrutherford
For quite a while I have been using the concept of base pages when developing pages in ASP.NET applications. It is a wonderful method for exposing common functions to all of your applications pages and also overriding certain events for various purposes (i.e. dynamic themes).
Recently I found out a new developer will be joining my team. This prompted me to review the applications code for readability and ease of maintenance. I began adding comments through out the code behind for all pages within the application. While doing so I noted that I had used common method names for such things as loading data, configuring controls, applying filters, etc.
Bringing a new developer on board, I wanted to make the transition as seamless as possible while also ensuring they follow existing coding practices we already have in place. While I could have created virtual methods for the common page methods allowing them to overridden, what I really needed was a way to ensure the new developer implemented the same methods for each and every page. Thus I created an interface to force the issue.
Now, every page not only inherits the base page class but also implements an interface. This provides every page not only common functions and overridden page events but also imposes rules for implementing certain common methods :-)
Interface
public interface BasePageInterface
{
/// Configures page based on users security permissions.
void CheckPermissions();
/// Configures Filter Form control for current page.
/// Ensure you have set the FilteredGrid and PageAjaxManager properties of the FilterForm control in PageLoad!!!
void ConfigureFilters();
/// Sets event handlers and default settings for controls on the current page.
void ConfigureControls();
/// Exports data bound to grid in selected format.
void ExportGridData(ExportFormat fmt);
/// Loads data and binds to grid.
/// Columns are turned on/off in grid depending on tab selected and users permissions.
void LoadData();
}
Page code-behind class definition:
public partial class MyPage : BasePage, BasePageInterface
Note, you could not use an abstract class to accomplish this considering C# does not allow for multiple inheritance. Nor could the base page class be abstract since it needs to inherit from the System.Web.UI.Page class in order to override page events.