+ All Categories
Home > Technology > ASP.NET Best Practices - Useful Tips from the Trenches

ASP.NET Best Practices - Useful Tips from the Trenches

Date post: 06-May-2015
Category:
Upload: habeeb-rushdan
View: 7,649 times
Download: 0 times
Share this document with a friend
Description:
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.
32
ASP.NET Best Practices - Useful Tips from the Trenches Habeeb Rushdan, MCT [email protected] om LowerHead
Transcript
Page 1: ASP.NET Best Practices - Useful Tips from the Trenches

ASP.NET Best Practices- Useful Tips from the Trenches

Habeeb Rushdan, [email protected] Consulting, LLC

Page 2: ASP.NET Best Practices - Useful Tips from the Trenches

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!

Page 3: ASP.NET Best Practices - Useful Tips from the Trenches

Agenda

IntroductionsBrief Introduction of ASP.NETBest Practice ExamplesMore Best Practice ExamplesA bit More Best Practice Examples…Useful Websites & ArticlesConclusion

Page 4: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 5: ASP.NET Best Practices - Useful Tips from the Trenches

A little bit about you…

Who are you?

I really want to know… (Sorry CSI, I couldn’t resist!)

Page 6: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 7: ASP.NET Best Practices - Useful Tips from the Trenches

Demo Time – Using Object Browser to look into System.Web Assembly’s Types

Page 8: ASP.NET Best Practices - Useful Tips from the Trenches

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!

Page 9: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 10: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 11: ASP.NET Best Practices - Useful Tips from the Trenches

Demo Time – Handling Page Events

Page 12: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 13: ASP.NET Best Practices - Useful Tips from the Trenches

Demo Time – Creating a WebSite

Page 14: ASP.NET Best Practices - Useful Tips from the Trenches

State Management Options in ASP.NET

ViewState

Session

Application

Cache

Page 15: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 16: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 17: ASP.NET Best Practices - Useful Tips from the Trenches

Demo Time - ViewState

Page 18: ASP.NET Best Practices - Useful Tips from the Trenches

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”

Page 19: ASP.NET Best Practices - Useful Tips from the Trenches

Cache

Robust Application-wide and Non-Session specific state management object

Provides many options for Item Expiration and Dependencies

Page 20: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 21: ASP.NET Best Practices - Useful Tips from the Trenches

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)

Page 22: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 23: ASP.NET Best Practices - Useful Tips from the Trenches

Demo Time – SessionManager Class

Page 24: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 25: ASP.NET Best Practices - Useful Tips from the Trenches

Exception Handling in ASP.NET

Web.config page redirect option

Page_Error

Application_Error

Page 26: ASP.NET Best Practices - Useful Tips from the Trenches

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

Page 27: ASP.NET Best Practices - Useful Tips from the Trenches

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!

Page 28: ASP.NET Best Practices - Useful Tips from the Trenches

Demo Time – Client side AJAX

Page 29: ASP.NET Best Practices - Useful Tips from the Trenches

Any Tips you would like to Add?

Come on, don’t be shy…

We won’t bite ya… we just had some free pizza!

Page 30: ASP.NET Best Practices - Useful Tips from the Trenches

What have you learned?

An Overview of ASP.NET

Several ASP.NET Best Practices

Page 31: ASP.NET Best Practices - Useful Tips from the Trenches

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/

Page 32: ASP.NET Best Practices - Useful Tips from the Trenches

Conclusion

Questions?/Comments…

As always, “Live long and code proper!”


Recommended