|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe advent of ASP.NET arriving in a world that up to that point had become accustomed to VBScript or JavaScript spaghetti code was the introduction of true, or truer, object oriented programming (OOP) paradigms to web page development. We have seen over the years how to apply objects to facilitate web components and controls within ASP.NET applications to automate the process of building out a functional web solution with minimal code. One of the most overlooked features of object orientation, however, is utilizing inheritance with the base The most important advantage base // Create a new .cs file with a CommonPage class declaration
public class CommonPage : System.Web.UI.Page
{
// ...
}
// Replace the standard inheritance declaration of any new web form
// with a reference to CommonPage
public class WebForm1 : CommonPage // changed ": System.Web.UI.Page"
{
// ...
}
By moving to our own base class type, we are now able to customize the base functionality of every web form that inherits this base type, simply by modifying CommonPage.cs. In this way, all pages subscribing to this base class can also be guaranteed to expose and make available certain properties and methods not as easily available in the previous base class, such as application-specific page, user, or session properties. We can also enforce security checks and clean-up routines that are common across all participating pages. Specifying Minimum Access LevelsThe natural initial progression of extending public CommonPage() {
base.Load +=new EventHandler(CommonPage_Load);
}
private void CommonPage_Load(object sender, EventArgs e) {
AccessCheck();
}
private void AccessCheck () {
// enforce security here ...
}
At this point, it is now possible to customize the access levels of each page with only a single line of code per page. In order to do this, we must first define a set of access levels with an public enum AccessLevel {
Guest = 0,
BaseMember = 1,
FullMember = 5, // numerically spaced for future expansion
Administrator = 10
}
With public class WebForm1 : CommonPage
{
protected override AccessLevel MinLevel {
get {
return AccessLevel.FullMember;
}
}
// ...
}
This is, of course, considered the most correct way of customizing an access level customization on an inherited web form. However, in order to make this only a single line of code, we can break the paradigm of using overridden properties and instead use a constant field. In our case, our goal is to eliminate as much coding as possible, including even any method or property blocks. So, we instead declare a simple field in the web form that specifies the minimum access level. public class WebForm1 : CommonPage
{
public const AccessLevel MinLevel = AccessLevel.FullMember;
// ...
}
In order for // Figure 1
public class CommonPage : System.Web.UI.Page
{
private const AccessLevel DefaultMinLevel = AccessLevel.Guest;
private void AccessCheck() {
AccessLevel minLevel = DefaultMinLevel;
System.Reflection.FieldInfo fi =
this.GetType().GetField("MinLevel",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.Static |
System.Reflection.BindingFlags.FlattenHierarchy);
if (fi != null) minLevel = (AccessLevel)fi.GetValue(null);
// ...
}
// ...
}
Now, Exposing a Common User ObjectNow that we have specified an access level for a page, we need to have // Figure 2
namespace InheritenceSample
{
public class AppUser
{
public AccessLevel Level = AccessLevel.Guest;
}
public class CommonPage : System.Web.UI.Page
{
// ...
public AppUser CurrentUser {
get {
AppUser usr = (AppUser)Session["AppUser"];
if (usr == null) {
usr = new AppUser();
Session.Add("AppUser", usr);
}
return usr;
}
}
}
}
At this point, it is very easy for if (CurrentUser.Level < minLevel) { // ...
By default, an Going FurtherIn addition to applying security, this paradigm makes all new pages derived from your We can also establish universal clean-up routines that are common to all pages in our application, by using the public class CommonPage : System.Web.UI.Page
{
public CommonPage() {
base.Load += new EventHandler(CommonPage_Load);
base.Unload += new EventHandler(CommonPage_Unload);
}
private void CommonPage_Unload(object sender, EventArgs e) {
// common clean up ...
}
// ...
}
All of the benefits of inheritance in object oriented programming apply in ASP.NET. Hopefully, we have only scratched the surface as to what levels of productivity you can take web application development to using inheritance in ASP.NET and making the most of OOP methodologies.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||