CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

jobs.internet.com

internet.commerce
Partners & Affiliates
Dental Insurance
Logo Design
PDA Phones & Cases
Corporate Gifts
Memory Upgrades
Hurricane Shutters
Career Education
Phone Cards
Server Racks
Laptops
Find Software
Promotional Gifts
Domain registration
Computer Deals


RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home >> .NET / C# >> .NET >> General ASP.NET >> Web Forms

Best Practices for Developing a Web Site: Checklists, Tips, Strategies & More. Download Exclusive eBook Now.

Practice for Designing Web Applications
Rating:

Ezhilan (view profile)
November 23, 2005

Introduction

This article gives some very basic approach for designing a scalable, maintainable, and extensible Web application using ASP.NET and OOP approach.

Defining the Base Classes for the Application


(continued)

Click Here

Download these IBM resources today!
e-Kit: IBM Rational Systems Development Solution
With systems teams under so much pressure to develop products faster, reduce production costs, and react to changing business needs quickly, communication and collaboration seem to get lost. Now, theres a way to improve product quality and communication.

Webcast: Asset Reuse Strategies for Success--Innovate Don't Duplicate!
Searching for, identifying, updating, using and deploying software assets can be a difficult challenge.

eKit: Rational Build Forge Express
Access valuable resources to help you increase staff productivity, compress development cycles and deliver better software, fast.

Download: IBM Data Studio v1.1
Effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life.

eKit: Rational Asset Manager
Learn how to do more with your reusable assets, learn how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse.

It's very common to have a mixture of Web pages in the application with some requiring proper user authentication to access the page and others may be accessible without proper authentication. Also, with more applications getting complex, there may be a requirement to have different levels of access to different parts of the application based on the user authentication level.

The following code sample illustrates how you can approach designing the Web application, using OOP to make the application more scalable and maintainable. In this example, consider a simple Web project with only one application. As said before, you can have a mixture of both secured Web pages (needs user authentication to access the page) and non-secured Web pages (can be accessed without user authentication). You can approach this by defining two base classes, both inheriting from System.Web.UI.Page, as shown below.

public class SecuredWebPage: System.Web.UI.Page
{
   public SecuredWebPage()
   {
   public void OnInit( System.Web.UI.Page Page)
   {
      if (Page.Session["CustomSession"] == null)
      {
         Page.Response.Redirect ("LogOn.aspx?CallingPage=" +
                                 Request.RawUrl );
      }
   }
}

public class NonSecuredWebPage: System.Web.UI.Page
{
   public NonSecuredWebPage()
   {}
   public void OnInit(System.Web.UI.Page Page)
   {
      if (Page.Session["GenericSession"] == null)
      {
         GenericSession genericSession = new GenericSession();
         genericSession.User = "Ananymous";
         Page.Session["GenericSession"] = genericSession;
      }
   )
}

These two base classes have the init event that is invoked from the pages that are used in the application passing the page itself as the argument. All you do here is check whether there is a valid Session Object available on the page. (For the non-secured pages, it's not necessary to check any session, or you can set some generic session that might be useful when navigating the application. The Session object mentioned in the code above can be defined as the class below.

Defining the Session Class

public class CustomSession
{
   private string mstrUser = "";
   private string mstrPassword = "";
   public CustomSession()
   {}
   public string User
   {
      get{ return mstrUser;}
      set {mstrUser = value;}
   }
   public string Password
   {
      get {return mstrPassword;}
      set {mstrPassword = value;}
   }
}
public class GenericSession
{
   B private string mstrUser = "";
   B public string User
   {
      get{ return mstrUser;}
      set {mstrUser = value;}
   }
}

The Session class as defined above will be more flexible, scalable, and maintainable rather than defining the session for each property defined in the class. By having the whole class in the session, the various properties of the session can be accessed from the page as customSession = (CustomSession) Session["CustomSession"];

The above base classes, along with the session class, checks for the valid Session object, If it is not available, redirect to a page called LogOnpage.aspx with the QueryString of the URL that is calling this page. This can be used to redirect to that page after a successful logon from the log on the page. (Otherwise, you can redirect to the default page on the application as per the business need.) In this example, you are redirecting the page to the page that called this logon page.

public class LogOn : System.Web.UI.Page
{
   private void Page_Load(object sender, System.EventArgs e)
   {
      private void btnOk_Click(object sender, System.EventArgs e)
      {
         //Validate and Authenticate the User credentials here..
         CustomSession customSession = new ();
         customSession.User = txtUser.Text;
         customSession.Password = txtPassword.Text;
         Session["CustomSession"] = customSession;
         Response.Redirect (strUrl);
         this.RegisterStartupScript ("MyFunction",
            "<script language='javascript'>
            window.returnValue = 'OK';window.close();</script>");
      }
   }
}

Defining the Web Pages in the Application

With these base classes in place, all your pages in the application now can be derived from either the SecuredWebPage class or the NonSecuredWebPage class with the authentication to the page implemented by default through these base classes. So, the actual authentication need not be included in each page. And, if there are any changes in the authentication or the session that need to be changed, the change can be done in one place with minimum code change and less chance of introducing any new bugs.

The code sample for the two types of pages in the application is as follows:

public class WebForm1 : Ez.SecuredWebpage
{
   private void Page_Load(object sender, System.EventArgs e)
   {
      CustomSession customSession =
         (CustomSession) Session["CustomSession"];
      Response.Write ("The Page is logged on with User - ");
      Response.Write(customSession.User);
      Response.Write ("<BR>");
      Response.Write ("and the password is ");
      Response.Write (customSession.Password);
   }
   override protected void OnInit(EventArgs e)
   {
      base.OnInit(this);
   }
}
public class WebForm3 : Ez.NonSecuredWebPage
{
   private void Page_Load(object sender, System.EventArgs e)
   {
      GenericSession genericSession =
         (GenericSession) Session["GenericSession"];
      Response.Write ("The Page is a non secured page logged
                       on with User - ");
      Response.Write(genericSession.User);
   }
   override protected void OnInit(EventArgs e)
   {
      base.OnInit(this);
   }
}

This above approach can be used as a template when designing a Web application.

Expanding the Design Scope Across Many Applications

Most of the applications in the real world are not going to be as simple as the sample application presented above. There may be lots of applications in the project and each with different levels of authentication level for a user. When the project gets more complicated, it will be a good practice to have the base classes and the logon project defined under a folder called, for example, Common and make that referenced to all the applications that are in the project group. And, it will be a good practice to have the UI and the Data/Utility Access Layer under different folders in the project. For example, you can have a project called B Project1 and have two folders inside that as ProjectUI where all the Web pages will reside, and ProjectMI folder where you can have all the Utility access/Data access done there. This will be a good practice to follow, making it easy to maintain the code in the long run. Also, by having the MI folder for each application, the above-explained base class inheritance can be further extended by defining the B base class for each folder, which in turn is derived from the Common Base class. By doing so, you can set the Attributes for the base class in the MI folder and then call the Common with those attributes. This way, you will be able to Set or Retrieve the Session object accordingly, as per the application.

Last but not least, when designing the complex application, it will be always a good practice to have the sub-folder properly defined and group the classes accordingly. For example, you should have a Utility Folder in the application that can have many classes to handle all the I/O or Registry operations and so forth by having classes such as Fileutiltiy.cs and RegistryUtility.cs. Also, you should have a Data class from where the Data Access operations should be handled. The page should always access its corresponding MI Class in the sub-folder, which in turn will access the Common/Utility/Data Access Layers. Defining and organizing the folders and files in the project will, in most cases, decide how the project can be scaled or maintained as the business requirement grows.

The above approach is an effort to define a scalable, maintainable Web application development implementing OOP. Depending on the need, this approach can be modularized more and developing complex Web applications can be achieved with less effort.

Conclusion

I hope the above explained approach will help a lot of developers who are in the initial phase of designing and developing either a simple or complex Web application and help develop the application more effectively.

About the Author
Over 12 years of experience in Software Development with Microsoft Technologies and Products. Microsoft Certified Solutions Developer MCSD in .NET and Visual Studio 6.0

Downloads

  • Ez.zip -

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed

    Data Sheet: IBM Information Server Blade
    Five Trends for Application Development & Program Management. Download Complimentary Report Now.
    Is it time to make your move to the multi-threaded and parallel processing world? Find out!
    Learn about expanding business opportunities for the reseller channel. Visit IT Channel Planet.
    Flash Demo: Learn how IBM Information Server Blade is easy to manage, highly scalable and efficient.


  • RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

    (You must be signed in to rank an article. Not a member? Click here to register)

    Latest Comments:
    No Comments Posted.
    Add a Comment:
    Title:
    Comment:
    Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



    (You must be signed in to comment on an article. Not a member? Click here to register)


    JupiterOnlineMedia

    internet.comearthweb.comDevx.commediabistro.comGraphics.com

    Search:

    Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

    Jupitermedia Corporate Info


    Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

    Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

    Solutions
    Whitepapers and eBooks
    Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
    Microsoft Article: 7.0, Microsoft's Lucky Version?
    Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
    Avaya Article: How to Feed Data into the Avaya Event Processor
    Microsoft Article: Install What You Need with Windows Server 2008
    HP eBook: Putting the Green into IT
    Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
    Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
    Avaya Article: Setting Up a SIP A/S Development Environment
    IBM Article: How Cool Is Your Data Center?
    Microsoft Article: Managing Virtual Machines with Microsoft System Center
    HP eBook: Storage Networking , Part 1
    Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
    MORE WHITEPAPERS, EBOOKS, AND ARTICLES
    Webcasts
    Intel Video: Are Multi-core Processors Here to Stay?
    On-Demand Webcast: Five Virtualization Trends to Watch
    HP Video: Page Cost Calculator
    Intel Video: APIs for Parallel Programming
    HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
    Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
    MORE WEBCASTS, PODCASTS, AND VIDEOS
    Downloads and eKits
    Sun Download: Solaris 8 Migration Assistant
    Sybase Download: SQL Anywhere Developer Edition
    Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
    Red Gate Download: SQL Compare Pro 6
    Iron Speed Designer Application Generator
    MORE DOWNLOADS, EKITS, AND FREE TRIALS
    Tutorials and Demos
    How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
    eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
    IBM Article: Collaborating in the High-Performance Workplace
    HP Demo: StorageWorks EVA4400
    Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
    Microsoft How-to Article: Get Going with Silverlight and Windows Live
    MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES