19
ASP.NET Best Practices For high Performance Applications Alok Kumar Pandey Director Strategic Product Development @BrainDigit IT Solution

Asp.net best practices

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Asp.net best practices

ASP.NET Best PracticesFor high Performance Applications

Alok Kumar PandeyDirector Strategic Product Development

@BrainDigit IT Solution

Page 2: Asp.net best practices

Talk about1. Introduction2. Plan and research before3. String concatenation4. Avoid round trips5. Save viewstate only when needed6. Session variables7. Server.Transfer8. Server controls when appropriate and avoid creating deeply nested controls9. Choose the data viewing control appropriate for your solution10. Optimize code and exception handling11. DataReader for fast and efficient data binding12. Paging efficiently13. Explicitly Dispose or Close all the resources14. Disable tracing and debugging15. Precompile pages and disable AutoEventWireup16. Stored procedures and indexes

Page 3: Asp.net best practices

IntroductionASP.NET is very powerful technology, however it is important to understand how to use that power to build highly efficient, reliable and robust applications. In this discussion we try to highlight the key tips that we can use to maximize the performance of our ASP.NET pages. The above list can be much longer, we will discus the most important ones.

Page 4: Asp.net best practices

Plan and research before• Research and investigate how .NET can really benefit you. • .NET offers a variety of solutions on each level of application design and

development.

Page 5: Asp.net best practices

String concatenation //using simple string concatenation string strName = string.Empty; for (int i = 0; i < 10; i++) { strName += "Name " + i; }

//Using StringBuilder StringBuilder strBuilder = new StringBuilder(); List<string> objNameList = new List<string>(); objNameList = null;//Set The name list foreach (string strName in objNameList) { strBuilder.Append(strName); }

Page 6: Asp.net best practices

Avoid round trips• Implement Ajax UI whenever possible• Use Client Side Scripts. Client site validation• Use Page.ISPostBack

Page 7: Asp.net best practices

Save viewstate only when neededViewState is used primarily by Server controls to retain state only on pages that post data back to themselves.

Situation when we do not need ViewState• Our page does not post back.• We do not handle server control events.• We repopulate controls with every page refresh.

Determine the size of your ViewState• By enabling tracing for the page, you can monitor the ViewState size for each

control

Page 8: Asp.net best practices

Session variables- Avoid storing too much data in session variables - Make sure your session timeout is reasonable.

To disable session state for a page, - Set the EnableSessionState attribute in the @ Page directive tofalse<%@ Page EnableSessionState="false" %>

To disable session state for a specific application - Use Web.config file of the application.

<sessionState mode='Off'/>

Page 9: Asp.net best practices

Server.TransferIf authentication and authorization check is needed duringredirection• Use Response.Redirect

To transfer to pages in other applications • Must use Response.Redirect

Use Server.Transfer to transfer control to pages in the same application• Helpful to show the message like exception etc.

Page 10: Asp.net best practices

Server controls when appropriate and avoid creating deeply nested controls• Nothing comes for free• The HTTP protocol is statelessBetter to replace a server control to html control if1. You do not need to retain state across postbacks2. The data that appears in the control is static or control displays

read-only data3. You do not need programmatic access to the control on the server-

side

Deeply nested hierarchies create extra processing like• Repeater, DataList, and DataGrid

Page 11: Asp.net best practices

Choose the data viewing control appropriate for your solution• Depend on how we choose to display data in a Web page• Always compare the pros and cons of controls before we use them in

our Web page

Page 12: Asp.net best practices

Optimize code and exception handling• To optimize expensive loops, • Use For instead of ForEach in performance-critical code paths. • Also do not rely on exceptions in code • Write code that avoids exceptions• Do not use exceptions to control logic• Check for null values, If it is possible for an object to be null

Page 13: Asp.net best practices

DataReader for fast and efficient data binding• Use a DataReader object if we not need to cache data• DataReader is the optimum choice for retrieving read-only data in a

forward-only

Page 14: Asp.net best practices

Paging efficiently• Retrieves only the desired data from the database• Reduces back-end work on the database

Page 15: Asp.net best practices

Explicitly Dispose or Close all the resources• Cleaned up when an exception occurs, use a try/finally block• Open connection just before need• Close it as soon as done with it• Make sure to call the Dispose or the Close method of the object if

provided

Page 16: Asp.net best practices

Disable tracing and debugging• Tracing and debugging are not recommended when our application is

running in production. • We can disable tracing and debugging in the Machine.config and

Web.config

Eg.

<configuration> <system.web> <trace enabled="false" pageOutput="false" /> <compilation debug="false" /> </system.web></configuration>

Page 17: Asp.net best practices

Precompile pages and disable AutoEventWireup• Precompiled pages increases the performance,• Slight performance boost by leaving the event wiring to the page

author instead of performing it automatically.

For example, we will need to override Page.OnLoad for the page load event instead of using a Page_Load method

Page 18: Asp.net best practices

Stored procedures and indexes• Use compiled stored procedures instead of RAW queries,• Proper indexes,• Reduce the chance of SQL Injection

Page 19: Asp.net best practices

Thank you