1 / 22

ASP.NET Best Practices

ASP.NET Best Practices. Dawit Wubshet Park University. StringBuilder. The StringBuilder class starts with a default initial capacity of 16. Strings less than the initial capacity are stored in the StringBuilder object.

Lucy
Télécharger la présentation

ASP.NET Best Practices

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.NET Best Practices Dawit Wubshet Park University

  2. StringBuilder • The StringBuilder class starts with a default initial capacity of 16. • Strings less than the initial capacity are stored in the StringBuilder object. • The initial capacity of the buffer can be set by using the following overloaded constructor: public StringBuilder (int capacity); For example: StringBuilder sb = new StringBuilder(2000);

  3. StringBuilder • Use StringBuilder when you need to concatenate strings multiple times. • // using String and '+' to append String str = "Some Text"; for ( ... loop several times to build the string ...) { str = str + " additional text "; } // using String and .Append method to append StringBuilder strBuilder = new StringBuilder("Some Text "); for ( ... loop several times to build the string ...) { strBuilder.Append(" additional text "); }

  4. StringBuilder • Use StringBuilder when the number of appends is unknown. • Code sample: for (int i=0; i< Results.Count; i++) { StringBuilder.Append (Results[i]); }

  5. StringBuilder • Treat StringBuilder as an accumulator or reusable buffer. • Avoids the allocations of temporary strings during multiple append iterations. Some of the scenarios where this helps are as follows: • Concatenating strings: You should always prefer the following approach to string concatenation when using StringBuilder. StringBuilder sb; sb.Append(str1); sb.Append(str2); Use the preceding code rather than the following. sb.Append(str1+str2);

  6. StringBuilder • Concatenating strings from various functions. Example: StringBuilder sb; sb.Append(f1(...)); sb.Append(f2(...)); sb.Append(f3(...)); The preceding code snippet results in temporary string allocations for the return values by the functions f1 (...), f2 (...), f3 (...). • This can be avoided by following this approach: void f1( sb,...); void f2( sb,...); void f3( sb,...);

  7. Round Trips to the Server • Round trips to a server needs to be reduced by: • Prevalidating data on the client-side. Use validation controls to implement client-side validation of user input. Example: <asp:requiredfieldvalidator id="rqrdUsername" runat="server" Display=“Dynamic" ControlToValidate="txtUsername"> Required.</asp:requiredfieldvalidator> • Using AJAX, Javascript.

  8. View State • View state is turned on in ASP.NET by default. • Disable view state if you do not need it. For example, you might not need view state because your page is output-only or because you explicitly reload data for each request. You do not need view state when the following conditions are true: ● Your page does not post back. ● You do not handle server control events. ● You repopulate controls with every page refresh.

  9. View State • Ways to disable view state at various levels: • To disable view state for all applications on a Web server, configure the <pages> element in the Machine.config file as follows: <pages enableViewState="false" /> To disable view state for a single control on a page, set the EnableViewState property of the control to false: //Run time yourControl.EnableViewState = false; //Design time <asp:datagrid EnableViewState="false" runat="server" /> • To disable view state for a single page, use the @ Page directive as follows: <%@ Page EnableViewState="false" %>

  10. Redirecting Between Pages • Where possible, use the Server.Transfer method instead of the Response.Redirect method. • If you need authentication and authorization checks during redirection, use Response.Redirect. • When you use Response.Redirect, ensure you use the overloaded method that accepts a Boolean second parameter: Response.Redirect(“Adminlogin.aspx”, false); • To transfer to pages in other applications, you must use Response.Redirect.

  11. Resource Acquisition & Disposing • Open critical, limited, and shared resources just before you need them, and release them as soon as you can. Critical, limited, and shared resources include resources such as database connections, network connections, and transactions. For example: conn.Open(); SqlCommand comm = new SqlCommand(“park_sp_GetAMember”, conn); Instead of: conn.Open(); restructureInput(); … SqlCommand comm = new SqlCommand(“park_sp_GetAMember”, conn);

  12. Resource Acquisition & Disposing • Use Try/Finally on disposable resources. To guarantee resources are cleaned up when an exception occurs, use a try/finally block. Close the resources in the finally clause: try { conn.Open(); ... } finally { if (null != conn) conn.close; }

  13. Pages • Trim your page size. • Enable buffering. • Ensure debug is set to false. • Optimize expensive loops.

  14. Pages • To trim your page size, you can do one or all of the following: ● Use script includes for any static scripts in your page to enable the client to cache these scripts for subsequent requests:<script language=jscript src="scripts\myscript.js"> ● Remove characters such as tabs and spaces that create white space before you send a response to the client. // with white space <table> <tr> <td>hello</td> <td>world</td> </tr> </table> The following sample table does not contain white spaces. // without white space <table> <tr><td>hello</td><td>world</td></tr> </table>

  15. Pages • If buffering is turned off, you can enable buffering by using the following methods: ● Enable buffering programmatically in a page. Response.BufferOutput = true; ● Enable buffering at the page level by using the @Page directive: <%@ Page Buffer = "true" %> ● Enable buffering at the application or computer level by using the <pages> element in the Web.config or Machine.config file: <pages buffer="true" ...>

  16. Pages • Ensure Debug is set to false: <compilation debug="false" ... /> • To set debug to false at the page level: <%@ Page debug="false" ... %> • When debug is set to true, the following occurs: ● Pages are not batch compiled. ● Pages do not time out. When a problem occurs, such as a problem with a Web service call, the Web server may start to queue requests and stop responding. ● Additional files are generated in the Temporary ASP.NET Files folder. ● The System.Diagnostics.DebuggableAttribute attribute is added to generated code. This causes the CLR to track extra information about generated code, and it also disables certain optimizations.

  17. Iterating and Looping • Avoid repetitive field or property access If you use data that is static for the duration of the loop, obtain it before the loop instead of repeatedly accessing a field or property. The following code shows a collection of orders being processed for a single customer. for ( int item = 0; item < Customer.Orders.Count ; item++ ){ CalculateTax ( Customer.State, Customer.Zip, Customer.Orders[item] ); } Instead: string state = Customer.State; string zip = Customer.Zip; int count = Customers.Orders.Count; for ( int item = 0; item < count ; item++ ) { CalculateTax (state, zip, Customer.Orders[item] ); }

  18. Iterating and Looping • Optimize or avoid expensive operations within loops Identify operations in your loop code that can be optimized. Look for code that causes boxing or allocations as a side effect. The following code causes side effect strings to be created for each pass through the loop. String str; Array arrOfStrings = GetStrings(); for(int i=0; i<10; i++) { str+= arrOfStrings[i]; } To avoid extra string allocations: StringBuilder sb = new StringBuilder(); Array arrOfStrings = GetStrings(); for(int i=0; i<10; i++) { sb.Append(arrOfStrings.GetValue(i)); }

  19. Iterating and Looping • Consider replacing recursion with looping since each recursive call adds data to the stack. Example: Array arr = GetArrayOfStrings(); int index = arr.Length-1; String finalStr= RecurStr(index); string RecurStr(int ind){ if (ind <= 0) return ""; else return (arr.GetValue(ind)+RecurStr(ind-1)); } Rewritten: string ConcString (Array array){ StringBuilder sb = new StringBuilder(); for (int i= array.Length; i>0; i--){ sb.Append(array.GetValue(i)); } return sb; }

  20. Iterating and Looping • Use for instead of foreach in performance-critical code paths since .NET uses an enumerator to provide enhanced navigation through arrays and collections. Instead of: for (int i=0; i<yourControl.Items.Count; i++){ … } Use: foreach (Control ctrl in yourControl) { … }

  21. Additional Resources • Microsoft Application Blocks: http://msdn2.microsoft.com/en-us/library/ms954836.aspx • FxCop: http://www.gotdotnet.com/team/fxcop/ • ASP.Net Analyzer: http://msdn2.microsoft.com/en-us/vstudio/aa718345.aspx

  22. Questions ???

More Related