Date post: | 06-May-2015 |
Category: |
Technology |
Upload: | habeeb-rushdan |
View: | 7,649 times |
Download: | 0 times |
ASP.NET Best Practices- Useful Tips from the Trenches
Habeeb Rushdan, [email protected] Consulting, LLC
Target Audience (Why should I sit through this session anyway?)
Programmers new to .NET Development
Any non-programmer(even those IT gals & guys) interesting in learning about ASP.NET Development
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!
Agenda
IntroductionsBrief Introduction of ASP.NETBest Practice ExamplesMore Best Practice ExamplesA bit More Best Practice Examples…Useful Websites & ArticlesConclusion
About your Presenter10 + Years working Professionally in the Technology FieldMicrosoft Certified Trainer (MCT)Microsoft Certified Professional Developer (MCPD: Web Developer)Microsoft Certified Technology Specialist (MCTS: .NET Framework 3.5, ASP.NET Applications)Microsoft Certified Technology Specialist (MCTS: .NET Framework 2.0: Web Applications)Microsoft Certified Application Developer (MCAD.NET)Adobe Certified Instructor - FlashAdobe Certified Expert (ACE - Flash CS3 Professional)Macromedia Certified Flash MX Developer
A little bit about you…
Who are you?
I really want to know… (Sorry CSI, I couldn’t resist!)
What is ASP.NET???
A series of Classes that live in the System.Web Assembly
Provides the ability to easily create dynamic websites and applications in the .NET Framework
Has all the benefits of OOP and the ability to access the thousands of classes built-in to Microsoft’s .NET Framework Class Library
Demo Time – Using Object Browser to look into System.Web Assembly’s Types
ASP.NET Page Execution Life-Cycle
A series of ASP.NET Page Events that occur in a specific order
Occurs every time you make a Request to an ASP.NET Page
Whether it is the first time you visit a page or any additional PostBack to the same page!
ASP.NET Page Life-Cycle Events
1. PreInit2. Init3. InitComplete4. PreLoad5. Load6. Control events
e.g Button1_Click, UserNameTextBox_TextChanged
7. LoadComplete8. PreRender9. SaveStateComplete10. Render11. Unload
Some of most commonly used ASP.NET Page Life-Cycle Events
PreInitSet a Master page or Theme dynamically
LoadSet properties in controls and grab data to be bound to controls that allow Data-binding
PreRenderMake final changes to the contents of the page or its controls e.g. attaching custom HTML attributes to a Button
Demo Time – Handling Page Events
Tips for Creating WebSites
Start with a Blank SolutionSeparate out your Application into logical Tiers
Separate Projects for UI, Business Rules, Data-Access, etc
Create a BasePage that other pages will inherit fromUse MasterPages for consistent layoutUse UserControls for reusable UI functionality
Demo Time – Creating a WebSite
State Management Options in ASP.NET
ViewState
Session
Application
Cache
ViewState
Maintains state at the Page/Control level
Is stored in a Hidden Form Input Element on the Client
It can get very large, very quickly so beware and disable it where possible
ASP.NET Control Tips
Don’t use a <ASP:Label> Server-side Tag when a caption will not be changed programmatically. Instead, a good Ole’ <Span> Client-side Tag will suffice
Disable ViewState in controls that don’t need to maintain their state during PostBacks
Demo Time - ViewState
Session
Maintains state at the Session level (generally speaking, per a user’s browser instance)
Items are accessible from Page to PageKeep in mind that Items stored In Session “linger” until they ExpireDon’t overuse or your web server’s memory will complain!Make sure any custom types you define that need to be stored in Session are marked “Serializable”
Cache
Robust Application-wide and Non-Session specific state management object
Provides many options for Item Expiration and Dependencies
Cache & Application Suggestions
Use the Cache Object instead of the legacy Application Object
Cache provides tons of more options for intelligently managing your application-wide state dataCompare the options available with Application.Add to the Cache.Add & Cache.Insert
State Management NO NOs
Don’t store unmanaged objects in State ManagementFor example:
No DataReadersNo File Handles (however, the contents of a file stored as System.String is OK)
State Management Suggestions
Always check for the existence of an object before accessing it (also called defensive programming)
Use string constants for keysThis prevents misspellings and other nasty side-effects
Consider using a “Manager” pattern with State Management objects
Demo Time – SessionManager Class
Some General Tips
Dispose of unmanaged resources after their useThis is especially important in web applications because of their disconnected natureTherefore, indulge the “using” statement
Make sure you have a robust exception handling strategy
Use Try/Catch/Finally where potential issues may occur and have a consistent logical way of dealing with exceptions
Exception Handling in ASP.NET
Web.config page redirect option
Page_Error
Application_Error
ASP.NET AJAX Options
Server-side AJAXUsing UpdatePanels with existing ASP.NET controls to “trick” the client that PostBacks are not occurring
Client-side AJAXNo trickery involved but more work & better bang for the buck
AJAX Tips
Minimize the use of Server-side AJAX and Update Panels
Bad for performance and may cause some unexpected results
Embrace Client-side AJAX (true AJAX)Microsoft makes it easy but you will need to learn some JavaScriptDon’t be scared… JavaScript is fun and exceptionally versatile!
Demo Time – Client side AJAX
Any Tips you would like to Add?
Come on, don’t be shy…
We won’t bite ya… we just had some free pizza!
What have you learned?
An Overview of ASP.NET
Several ASP.NET Best Practices
Useful Websites & Articles
Microsoft’s Official Developer Network Sitehttp://www.msdn.com
ASP.NET Official Web Sitehttp://www.asp.net/http://www.asp.net/ajax/
CodeProject Web Sitehttp://www.codeproject.com/
Conclusion
Questions?/Comments…
As always, “Live long and code proper!”