Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ASP.NET Best Practices - Useful Tips from the Trenches


Published on

Slides from a presentation Habeeb Rushdan, MCT gave at the NoVa CodeCamp 2009.02 held at Microsoft Offices in Reston, VA on October 10, 2009. The session abstract follows:

This session will focus on some key ASP.NET web development best practices that are commonly overlooked. We will dig into ASP.NET from an Object Oriented Programming perspective and explore how this enables us to create easily maintainable and scalable applications. If you have a Classic ASP or web scripting background and have recently transitioned to ASP.NET or have been thinking about making the transition to ASP.NET, this session will get you started on the right path. Also, if you have been developing in ASP.NET for a while and are looking for some useful tips, be sure to stop by as well. I will pull from my years of experience as a software development consultant and MCT working with numerous development teams to help promote code that works as expected and is easily maintainable. This session will be geared towards ASP.NET WebForm development. Sorry ASP.NET MVC, due to presentation time limitations you couldn’t be included in the roster this time around.

Published in: Technology
  • Be the first to comment

ASP.NET Best Practices - Useful Tips from the Trenches

  1. 1. ASP.NET Best Practices - Useful Tips from the Trenches<br />HabeebRushdan, MCT<br /><br />LowerHead Consulting, LLC<br />
  2. 2. Target Audience (Why should I sit through this session anyway?)<br />Programmers new to .NET Development<br />Any non-programmer(even those IT gals & guys) interesting in learning about ASP.NET Development<br />Existing ASP.NET web developers interesting in learning a few best practices… we only have a little over an hour so we can’t cover too much!<br />
  3. 3. Agenda<br />Introductions<br />Brief Introduction of ASP.NET<br />Best Practice Examples<br />More Best Practice Examples<br />A bit More Best Practice Examples…<br />Useful Websites & Articles<br />Conclusion<br />
  4. 4. About your Presenter<br />10 + Years working Professionally in the Technology Field<br />Microsoft Certified Trainer (MCT)<br />Microsoft Certified Professional Developer (MCPD: Web Developer)<br />Microsoft Certified Technology Specialist (MCTS: .NET Framework 3.5, ASP.NET Applications)<br />Microsoft Certified Technology Specialist (MCTS: .NET Framework 2.0: Web Applications)<br />Microsoft Certified Application Developer (MCAD.NET)<br />Adobe Certified Instructor - Flash<br />Adobe Certified Expert (ACE - Flash CS3 Professional)<br />Macromedia Certified Flash MX Developer<br />
  5. 5. A little bit about you…<br />Who are you?<br />I really want to know… <br />(Sorry CSI, I couldn’t resist!)<br />
  6. 6. What is ASP.NET???<br />A series of Classes that live in the System.Web Assembly<br />Provides the ability to easily create dynamic websites and applications in the .NET Framework<br />Has all the benefits of OOP and the ability to access the thousands of classes built-in to Microsoft’s .NET Framework Class Library<br />
  7. 7. Demo Time – Using Object Browser to look into System.Web Assembly’s Types<br />
  8. 8. ASP.NET Page Execution Life-Cycle<br />A series of ASP.NET Page Events that occur in a specific order<br />Occurs every time you make a Request to an ASP.NET Page<br />Whether it is the first time you visit a page or any additional PostBack to the same page!<br />
  9. 9. ASP.NET Page Life-Cycle Events <br />PreInit<br />Init<br />InitComplete<br />PreLoad<br />Load<br />Control events <br />e.g Button1_Click, UserNameTextBox_TextChanged<br />LoadComplete<br />PreRender<br />SaveStateComplete<br />Render<br />Unload<br />
  10. 10. Some of most commonly used ASP.NET Page Life-Cycle Events <br />PreInit<br />Set a Master page or Theme dynamically<br />Load<br />Set properties in controls and grab data to be bound to controls that allow Data-binding<br />PreRender<br />Make final changes to the contents of the page or its controls e.g. attaching custom HTML attributes to a Button<br />
  11. 11. Demo Time – Handling Page Events<br />
  12. 12. Tips for Creating WebSites<br />Start with a Blank Solution<br />Separate out your Application into logical Tiers<br />Separate Projects for UI, Business Rules, Data-Access, etc<br />Create a BasePage that other pages will inherit from<br />Use MasterPages for consistent layout<br />Use UserControls for reusable UI functionality<br />
  13. 13. Demo Time – Creating a WebSite<br />
  14. 14. State Management Options in ASP.NET<br />ViewState<br />Session<br />Application<br />Cache<br />
  15. 15. ViewState<br />Maintains state at the Page/Control level<br />Is stored in a Hidden Form Input Element on the Client <br />It can get very large, very quickly so beware and disable it where possible <br />
  16. 16. ASP.NET Control Tips<br />Don’t use a &lt;ASP:Label&gt; Server-side Tag when a caption will not be changed programmatically. Instead, a good Ole’ &lt;Span&gt; Client-side Tag will suffice<br />Disable ViewState in controls that don’t need to maintain their state during PostBacks<br />
  17. 17. Demo Time - ViewState<br />
  18. 18. Session<br />Maintains state at the Session level (generally speaking, per a user’s browser instance)<br />Items are accessible from Page to Page<br />Keep in mind that Items stored In Session “linger” until they Expire<br />Don’t overuse or your web server’s memory will complain!<br />Make sure any custom types you define that need to be stored in Session are marked “Serializable”<br />
  19. 19. Cache<br />Robust Application-wide and Non-Session specific state management object<br />Provides many options for Item Expiration and Dependencies<br />
  20. 20. Cache & Application Suggestions<br />Use the Cache Object instead of the legacy Application Object<br />Cache provides tons of more options for intelligently managing your application-wide state data<br />Compare the options available with Application.Add to the Cache.Add & Cache.Insert<br />
  21. 21. State Management NO NOs<br />Don’t store unmanaged objects in State Management<br />For example:<br />No DataReaders<br />No File Handles (however, the contents of a file stored as System.String is OK)<br />
  22. 22. State Management Suggestions<br />Always check for the existence of an object before accessing it (also called defensive programming)<br />Use string constants for keys<br />This prevents misspellings and other nasty side-effects<br />Consider using a “Manager” pattern with State Management objects<br />
  23. 23. Demo Time – SessionManager Class<br />
  24. 24. Some General Tips<br />Dispose of unmanaged resources after their use<br />This is especially important in web applications because of their disconnected nature<br />Therefore, indulge the “using” statement<br />Make sure you have a robust exception handling strategy<br />Use Try/Catch/Finally where potential issues may occur and have a consistent logical way of dealing with exceptions<br />
  25. 25. Exception Handling in ASP.NET<br />Web.config page redirect option<br />Page_Error<br />Application_Error<br />
  26. 26. ASP.NET AJAX Options<br />Server-side AJAX<br />Using UpdatePanels with existing ASP.NET controls to “trick” the client that PostBacks are not occurring<br />Client-side AJAX<br />No trickery involved but more work & better bang for the buck<br />
  27. 27. AJAX Tips<br />Minimize the use of Server-side AJAX and Update Panels<br />Bad for performance and may cause some unexpected results<br />Embrace Client-side AJAX (true AJAX)<br />Microsoft makes it easy but you will need to learn some JavaScript<br />Don’t be scared… JavaScript is fun and exceptionally versatile!<br />
  28. 28. Demo Time – Client side AJAX<br />
  29. 29. Any Tips you would like to Add?<br />Come on, don’t be shy…<br />We won’t bite ya… we just had some free pizza!<br />
  30. 30. What have you learned?<br />An Overview of ASP.NET<br />Several ASP.NET Best Practices<br />
  31. 31. Useful Websites & Articles<br />Microsoft’s Official Developer Network Site<br /><br />ASP.NET Official Web Site<br /><br /><br />CodeProject Web Site<br /><br />
  32. 32. Conclusion<br />Questions?/Comments…<br />As always, “Live long and code proper!”<br />