Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Some Best Practices for Improving Performance and Scalability

0.00/5 (No votes)
30 Nov 2005 1  
This articles details some best practices for improving performance and scalability.

Introduction

This articles describes ways to improve your application's performance and scalability.

Using the code

Session

Remarks (from MSDN)

This property provides information about the current request's session. A Session object is maintained for each user that requests a page or document from an ASP.NET application. Variables stored in the Session object are not discarded when the user moves from page to page in the application; instead, these variables persist as long as the user is accessing pages in your application.

Remarks (from myself)

The Session can be a very powerful object to use but can also be very dangerous to use. I will explain why, below.

Common Use for a Session Variable
Session["UserID"] = this.txtUserID.Text; 
Session["Password"] = this.txtPassword.Text; 
Session["Age"] = this.txtAge.Text; 
Session["Name"] = this.txtNameID.Text; 
Session["LastName"] = this.txtLastName.Text; 
Session["PhoneNumber"] = this.txtPhoneNumber.Text; 
Session["Email"] = this.txtEmail.Text;

Now, my problem is if you get lots of pages you are going to get lost regarding where you used / created session variables. And before you know, you go and create another variable for the same information and now you have got two session variables storing the same info. You create more session variables on other pages and so on. Now you have got session variables all over the place eating up your resources.

My Solution
public class MySessionInfo 
{ 
    public string UserID; 
    public string Password; 
    public string Age; 
    public string Name; 
    public string LastName;
    public string PhoneNumber; 
    public string Email; 
}

My solution is to create a class storing all the required information and storing a single object of that class in the session. Therefore, you will know exactly what is stored in the session! This also helps if you have more than one developer working on a project. They can see if you have already got a session variable for storing a particular information, and so they don't go create a duplicate for it. You will see how to easily access the session this way when I explain my base class.

Base Class (System.Web.UI.Page)

Remarks (From MSDN)

System.Web.UI.Page represents an .aspx file, also known as a Web Forms page, requested from a server that hosts an ASP.NET Web application.

Inheritance

In addition to user controls, you can use inheritance as a technique to extend the functionality of your ASP.NET mobile Web applications. If you create a class that inherits from an existing ASP.NET mobile control class, you can add additional functionality by overriding existing members or by creating new properties, methods, and events for the new class.

Remarks (from myself)

Lots of new (ASP.NET) developers don't really think about using inheritance. What I am going to do is create a base class and then make my pages inherit from my base class. The advantage of this is that you can manage all global variables in one class. The disadvantage of this is that you need to compile the class before you can view the pages in design time.

Standard Inheritance
public class WebForm1 : System.Web.UI.Page
My Inheritance
public class WebForm1 : MyBasePage
public class MyBasePage : System.Web.UI.Page 
{ 
    public MySessionInfo CurrentUserSession 
    { 
        get 
       { 
             if (Session["MySessionInfo"] == null) 
                 Session["MySessionInfo"] = new MySessionInfo(); 
             return Session["MySessionInfo"] as MySessionInfo; 
       } 
       set 
       { 
           Session["MySessionInfo"] = value; 
        }  
}

In the above code, you can see that I used the session object I created.

public class WebForm1 : MyBasePage
{
     private void Page_Load(object sender, System.EventArgs e)
     {
         this.CurrentUserSession.UserID = "NewUser"; 
     }
}
public class WebForm2 : MyBasePage
{
     private void Page_Load(object sender, System.EventArgs e)
     {
         this.txtUserID.Text = this.CurrentUserSession.UserID
     }
}

You can see that I only created a single session variable in my base class. Due to the fact that I'm inheriting from my base class, I can use the session with out typing Session[""]. In my base class, I also created an OpenConnection() method where I return a DataConnection object that is open and a CloseConnection() method that closes the DataConnection and disposes off the object:

public class MyBasePage : System.Web.UI.Page 
{ 
    protected System.Data.SqlClient.SqlConnection connection; 
    public System.Data.SqlClient.SqlConnection OpenConnection() 
    { 
        if (connection == null) 
            connection = 
             new System.Data.SqlClient.SqlConnection("myConnectionString"); 
        if (connection.State != System.Data.ConnectionState.Open) 
            connection.Open(); 
        return connection; 
    } 
    public void CloseConnection() 
    { 
        if (connection != null) 
        { 
            if (this.connection.State != 
                System.Data.ConnectionState.Closed) 
            { 
                this.connection.Close(); 
                this.connection.Dispose(); 
            } 
        } 
    } 
}

An example of using this feature:

public class WebForm2 : MyBasePage
{
     private void Page_Load(object sender, System.EventArgs e)
     {
         System.Data.SqlClient.SqlCommand Command = 
                new System.Data.SqlClient.SqlCommand();
         Command.Connection = this.OpenConnection();
         //Do Somthing

         this.CloseConnection();
         this.Command.Dispose();

     }
}

There are lots more you can do with the base class. Example: if you want to write the date on each page, you just do it in your base class' Onload method and all your pages will automatically display the date. Validation can be done now in one place and all pages that inherit from the base automatically have the validation functions.

Session and Transfer to a New Page

Remarks (From MSDN)

You may want to redirect users from one Web Forms page to another page. You might do this to display a page that is matched to the user's browser capabilities or is written in the language that the user speaks. There are two ways to redirect pages:

  • Using a server-side method.

    In this scenario, the server simply transfers the context to another page. The advantage is that you can share page context information between pages. The disadvantage is that the user's browser does not know about the transfer, so the browser's history is not updated. If the user refreshes the page, unexpected results can occur.

  • Using the browser.

    In this scenario, you send a command to the user's browser that causes the browser to fetch a different page. The advantage is that the browser's history is updated. The disadvantage is that this scenario performs an extra round trip, which can affect performance. Context.Handler encapsulates all HTTP-specific information about an individual HTTP request.

Remarks (from myself)

What lots of people do is if you want to do a transfer, let's say from a Browse to an Update screen, the developer stores the selected row in the session, redirects/transfers to the other page and the open up the session. You don�t actually have to store the info into the session. You can transfer it to the other page. Note: This trick only works if you use the Transfer method.

public class WebForm1 : MyBasePage
{
     public string MyValue;
     private void Page_Load(object sender, System.EventArgs e)
     {
         Myvalue = "This is a Value From WebForm1";
         Server.Transfer("WebForm2.aspx");
     }
}
public class WebForm2 : MyBasePage
{
     private void Page_Load(object sender, System.EventArgs e)
     {
         this.txtMyValue.Text = ((WebForm1)Context.Handler).MyValue;

     }
}

Finally

Here's a great article you might be interested in:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here