best practices for.net development thom robbins [email protected]

32
Best Practices for .NET Development Thom Robbins [email protected]

Upload: elizabeth-ferguson

Post on 11-Jan-2016

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Best Practices for .NET Development

Thom [email protected]

Page 2: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

What we will cover Design Guidelines Memory Management Data Access Internet Services Threading Security

Page 3: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Session Prerequisites

Know VB .NET or C# Be familiar with .NET Base Class Libraries Be familiar with XML

Level 300Level 300

Page 4: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

So Why This Presentation? You know you are a VB programmer if…

You ever had to use the On Error Goto statement You never wrote a multi-threaded app

You know you are a C++ programmer if… You ever had to check an HRESULT every 2 lines

of code 30% of your code was releasing objects from

memory You know you are an ADO programmer if…

You had to convert between a Recordset and a DOM and transform the XML 5 times in between

Page 5: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Agenda

.NET Design Guidelines Memory Management Data Access Internet Services Threading Security

Page 6: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

.NET Design GuidelinesNaming Conventions

Hungarian notation is out! For public interfaces, use PascalCasing For private members, use camelCasing Use underscore “_” character to denote

private class members Use camelCasing for all method

parameters

Page 7: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

.NET Design GuidelinesNaming Conventions

public class Customer{ private string _password;

public void SetPassword(string newPassword) { _password = newPassword; }}

Page 8: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

.NET Design GuidelinesClass Members Usage

Don’t use public fields, use properties No write-only methods, use a method Only use properties for setting and

retrieving values Allow properties to be set in any order Use a consistent ordering and naming

pattern for parameters

Page 9: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

.NET Design GuidelinesBase Classes vs. Interfaces

Only Use Interfaces When… Unrelated classes want to support a protocol Aggregation is not appropriate

Provide class customization through protected methods

Page 10: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

.NET Design GuidelinesError Raising and Handling

Exceptions are not for flow of control! Exceptions are “exceptional” Derive new custom exceptions from the

ApplicationException class

Page 11: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Agenda

.NET Design Guidelines Memory Management Data Access Internet Services Threading Security

Page 12: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Memory Management Avoid Finalize() Only use Finalize() with Dispose()

public void Dispose(){ // Clean up unmanaged resources GC.SuppressFinalize(this);}

protected override void Finalize(){ // Clean up unmanaged resources base.Finalize();}

Page 13: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Agenda

.NET Design Guidelines Memory Management Data Access Internet Services Threading Security

Page 14: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Data AccessAccessing Relational Data

Always use the optimal Managed Provider Pick DataReader over DataSet when

possible Used stored procedures when possible Do NOT use dynamic connection strings

Page 15: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Data AccessXML Data

Use the XmlDataDocument for XML/DataSet integration DOM DataSet DOM

Don’t use DOM if you don’t need it Only necessary for in-memory editing

XmlReader is faster than DOM

Page 16: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Agenda

.NET Design Guidelines Memory Management Data Access Internet Services Threading Security

Page 17: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Internet ServicesWebClient vs. WebRequest

Use WebClient for simple request and response operations

Use WebRequest for more complex operations Asynchronous requests, setting headers, etc.

Page 18: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Internet ServicesGeneral Tips

Don’t pass credentials every time Don’t type cast to descendant classes,

such as HttpRequest In ASP.NET, use the asynchronous

methods of GetResponse and GetResponseStream

As a good starting point, use 8 connections/processor

Page 19: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Agenda

.NET Design Guidelines Memory Management Data Access Internet Services Threading Security

Page 20: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

ThreadingGeneral Tips

Avoid locks whenever possible Don’t provide static methods that alter

static state Asynchronous invocation via delegates

are the preferred threading mechanism

Page 21: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

ThreadingSynchronization

Starvation is caused by multiple threads contending for a resource

The Monitor and ReaderWriterLock are designed to prevent starvation

Page 22: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Agenda

.NET Design Guidelines Memory Management Data Access Internet Services Threading Security

Page 23: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

SecurityKey Concepts

Use the principal of least privilege Don’t run Visual Studio with admin

privileges Use the runas utilityC:\>runas /user:timmc\administrator cmd

Enter password for timmc\administrator:

Lock down security policy early

Page 24: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

SecurityCode Access Security

Access to a protected resource The ability to perform a protected

operation

FileIOPermission permission = new FileIOPermission(PermissionState.None);permission.AllLocalFiles = FileIOPermissionAccess.Read;

Page 25: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

SecurityRole-Based Security

Imperative (old way)

public void DoTransaction(){ IPrincipal principal = Thread.CurrentPrincipal; if (!principal.IsInRole("Managers")) { throw new SecurityException("Not a " + "manager!"); } // OK, do the transaction...}

Page 26: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

SecurityRole-Based Security

Imperative (new way)

public void DoTransaction(){ PrincipalPermission permission = new PrincipalPermission(null, "Managers"); permission.Demand(); // Now do the transaction...}

Page 27: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

SecurityRole-Based Security

Declarative

[PrincipalPermission(SecurityAction.Demand, Role="Managers")]void DoTransaction(){ // this time, really // do the transaction...}

Page 28: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

Session Summary Write consistent and predictable code Write scalable, high-performance code Write secure code

Page 29: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

For More Information… MSDN Web site at

msdn.microsoft.com

MSDN Magazine http://msdn.microsoft.com/msdnmag/

Page 30: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

For More Information… Microsoft Visual Studio .NET Documentation

http://msdn.microsoft.com/library/default.asp?url=/nhp/Default.asp?contentid=28000451

Page 31: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com

MS PressEssential Resources for Developers

To find the latest developer related titles visitTo find the latest developer related titles visit

www.microsoft.com/mspresswww.microsoft.com/mspress

Page 32: Best Practices for.NET Development Thom Robbins trobbins@microsoft.com