Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ASP.Net - Best Practices
#1

[attachment=14359]
ASP.NET is much more powerful than Classic ASP, however it s important to understand how to use that power to build highly efficient, reliable and robust applications. In this article I tried to highlight the key tips you can use to maximize the performance of your ASP.NET pages. The list can be much longer, I am only emphasizing the most important ones.
1. Plan and research before you develop: Research and investigate how .NET can really benefit you. .NET offers variety of solutions on each level of application design and development. It is imperative that you understand your situation and pros and cons of each approach supported by this rich development environment. Visual Studio is a comprehensive development package and offers many options to implement the same logic. It is really important that you examine each option and find the best optimal solution suited for the task at hand. Use layering to logically partition your application logic into presentation, business, and data access layers. It will not only help you create maintainable code, but also permits you to monitor and optimize the performance of each layer separately. A clear logical separation also offers more choices for scaling your application. Try to reduce the amount of code in your code-behind files to improve maintenance and scalability.
2. String concatenation: If not handled properly, String Concatenation can really decrease the performance of your application. You can concatenate strings in two ways. First, using string and adding the new string to an existing string. However, this operation is really expensive (especially if you are Concatenating the string within a loop). When you add a string to an existing string, the Framework copies both the existing and new data to the memory, deletes the existing string, and reads data in a new string. This operation can be very time consuming and costly in lengthy string concatenation operations. The second and better way to concatenate strings is using the StringBuilder Class. Below is an example of both approaches. If you are considering doing any type of Sting Concatenation, please do yourself a favor and test both routines separately. You may be surprised at the results.
Code:
Concatenation using String Class
Response.Write("<b>String Class</b>")
Dim str As String = ""
Dim startTime As DateTime = DateTime.Now
Response.Write(("<br>Start time:" + startTime.ToString())
Dim i As Integer
For i = 0 To 99999
str += i.ToString()
Next i
Dim EndTime As DateTime = DateTime.Now
Response.Write(("<br>End time:" + EndTime.ToString())
Response.Write(("<br># of time Concatenated: " + i.ToString))
Results: Took 4 minutes and 23 Seconds to to complete 100,000 Concatenations.
Code:
String Class
Start time:2/15/2006 10:21:24 AM
End time:2/15/2006 10:25:47 AM
# of time Concatenated: 100000
'Concatenation using StringBuilder
Response.Write("<b>StringBuilder Class</b>")
Dim strbuilder As New StringBuilder()
Dim startTime As DateTime = DateTime.Now
Response.Write(("<br>Start time:" + startTime.ToString())
Dim i As Integer
For i = 0 To 99999
strbuilder.Append(i.ToString())
Next i
Dim EndTime As DateTime = DateTime.Now
Response.Write(("<br>Stop time:" + EndTime.ToString())
Response.Write(("<br># of time Concatenated: " + i.ToString))
Results: Took less than a Second to complete 100,000 Concatenations.
StringBuilder Class
Start time:2/15/2006 10:31:22 AM
Stop time:2/15/2006 10:31:22 AM
# of time Concatenated: 100000
This is one of many situations in which ASP.NET provides extremely high performance benefits over classis ASP.
3. Avoid round trips to the server: You can avoid needless round trips to the Web Server using following tips.
Implement Ajax UI whenever possible. The idea is to avoid full page refresh and only update the portion of the page that needs to be changed. I think Scott's article gave great information on how to implement Ajax Atlas and <atlas:updatepanel> control.
Use Client Side Scripts. Client site validation can help reduce round trips that are required to process user s request. In ASP.NET you can also use client side controls to validate user input.
Use Page.ISPostBack property to ensure that you only perform page initialization logic when a page first time loaded and not in response to client postbacks.
If Not IsPostBack Then
LoadJScripts()
End If
In some situations performing postback event handling are unnecessary. You can use client callbacks to read data from the server instead of performing a full round trip
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Powered By MyBB, © 2002-2024 iAndrew & Melroy van den Berg.