Contents

Introduction xxix

Part I: Design Strategies 1

Chapter 1: Defi ning the Language Environment 3 Defi ning the Design Strategy Elements 4 Considering the C# Language 5 Understanding the Benefi ts 5 Understanding the Defi ciencies 8 Developing with Multiple Languages 10 Multiple Platform Scenarios 11 Inventorying Your Tools 12 Gathering Your Resources 12 Inventorying Team Language Resources 13 Considering Team Skills 13 Understanding the Signifi cance of Experience 14 Defi ning External Resource Requirements 14 Developing Your Design Strategy 15

Chapter 2: Understanding the Application Lifecycle 1 7 Understanding the Lifecycle Stages 18 Developing an Application Concept 18 Building an Application Development Team 19 Defi ning a Specifi cation 21 Determining Whether the Concept Will Work 22 Creating a DesignCOPYRIGHTED MATERIAL 24 Testing the Design 26 Implementing the Design 27 Writing the Application 28 Testing User Requirements 31 Enhancing Reliability and Security 32 Deploying the Application 33 Supporting and Maintaining the Application 34 Retiring the Application 35

115962ftoc.indd5962ftoc.indd xvxv 11/22/09/22/09 110:42:090:42:09 AMAM Contents

Considering a Lifecycle Model 35 Using the Spiral Model 36 Using the Waterfall Model 38 Using the Throwaway Model 40 Using the Evolutionary Model 41 Using the Incremental/Iterative Model 42 Using the Rapid Application Development (RAD) Model 43 Using Agile Programming Techniques 46 Understanding Agile Programming 46 Benefi ts of Agile Programming 47 Defi ciencies of Agile Programming 48 Common Agile Programming Strategies 48 Developing Your Design Strategy 54

Chapter 3: Defi ning a Design Strategy 5 5 Creating an Object Model 56 Defi ning the Class Concepts 56 Developing Internal Classes 60 Adding External Classes 61 Considering Class Trade-offs 62 Improving the Class Design 63 Building a Data Model 64 Defi ning the Data Requirements 64 Considering Access, Reliability, and Security Requirements 66 Creating the Data Classes 67 Considering User Requirements 67 Defi ning the User Requirements 68 Creating the User Classes 70 Turning Your Design into UML 70 Getting Started with Visio 71 An Overview of the Visio UML Functionality 72 Generating Code from UML 75 Developing Your Design Strategy 77

Chapter 4: Designing the User Interface 7 9 Understanding the Application Form Types 80 Creating a Dialog Utility 80 Interacting with the Single Document Interface (SDI) 82 Interacting with the Multiple Document Interface (MDI) 84 Relying on a Tabbed Interface 85 Developing Specialty Skinned and Free-Form Applications 86

xvi

115962ftoc.indd5962ftoc.indd xvixvi 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Working with RibbonX Applications 87 Using the Notifi cation Area 89 Investigating the Command Line 90 Understanding the Common User Interface Types 91 Menu Driven 91 Task Driven 92 Information Driven 92 Wizard Driven 93 Role Driven 94 Developing a User Interaction Strategy 95 Keeping the User in Control 96 Nudging the User in the Right Direction 97 Designing an Interface That Interacts 97 Defi ning a Reliable Interface 98 Considering Security Concerns 98 Considering Accessibility Requirements 99 Understanding Visual Element Considerations 100 Adding Tooltips and Accessibility Descriptions 101 Practicing Good Coloring Techniques 102 Developing Your Design Strategy 103

Chapter 5: Designing with Speed in Mind 10 5 Considering Speed vs. Performance 106 Developing a High Speed Application 107 Using Multithreading 107 Understanding That Speed Doesn’t Equal Code 108 Considering the Effects of Multiprocessing 109 Measuring Application Speed 109 Viewing Standard Counters 109 Defi ning an Application Counter 115 Developing Your Design Strategy 119

Chapter 6: Designing with Reliability in Mind 12 1 Verifying Resource Availability 122 Saving Data, Settings, and State 124 Implementing Data Saves 124 Considering Methods to Save Settings 125 Implementing State Saves 127 Expecting the Unexpected 128 Considering the Reliability Benefi ts of RibbonX 129 Developing Your Design Strategy 130

xvii

115962ftoc.indd5962ftoc.indd xviixvii 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Chapter 7: Designing with Security in Mind 13 1 Assuming the Worst-Case Scenario 132 Eliminating Errant Input 133 Using the Correct Control or Component 134 Checking All User Input 135 Verifying Data Sources 142 Hiding Data from View 142 Keeping Data Local 143 Encrypting Data 143 Placing Data in Protected Locations 144 Adding Application Monitoring 145 Defi ning the Monitoring Strategy 145 Implementing Logging 146 Using the Event Log 147 Sending Administrator Alerts 148 Using a Team Overview 149 Developing Your Design Strategy 150 Part II: Coding Strategies 15 3

Chapter 8: Customizing the IDE 15 5 Confi guring the IDE 156 Using the Visual Studio Options 156 Customizing the Toolbars and Menus 159 Adding External Tools 164 Using Snippets, Macros, and Add-ins 165 Creating and Using Code Snippets 166 Using Macros to Automate Tasks 168 Relying on Add-ins for Extended Functionality 168 Using the Visual Studio Command Line 169 Accessing the Command Line 169 Understanding the Command Line Switches 170 Executing Visual Studio Commands 174 Running your Application 175 Coding Your Application 175

Chapter 9: Scripting 17 7 Considering the Scripting Options 177 Working with SQL Server 178 Creating C# Scripts 178

xviii

115962ftoc.indd5962ftoc.indd xviiixviii 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Evaluating C# Expressions 179 Using Windows PowerShell 179 Using LINQ 180 Interacting with SQL Server 181 Working from Visual Studio 181 Using Built-in SQL Server Features 183 Scripting Your Application 184 Using C# Expressions 185 Developing Windows PowerShell Solutions 185 Understanding the PS Utility 186 Creating a Script 187 Executing the Script 190 Obtaining Debugger Support 191 Coding Your Application 191

Chapter 10: Viewing Data in the IDE 19 3 Working with the IDE Elements 194 Using the Autos Window 194 Using the Locals Window 195 Using the Watch Window 196 Using the Command Window 197 Understanding the Use of Visualizers 197 Defi ning a Need for Visualizers 198 Using the Text Visualizer 198 Using the XML Visualizer 199 Using the HTML Visualizer 200 Using the DataSet Visualizer 201 Obtaining Third-Party Visualizers 202 Creating a Custom Visualizer 202 Confi guring the Visualizer Project 202 Adding the Visualizer Code 204 Testing the New Visualizer 207 Coding Your Application 209

Chapter 11: Working with Controls and Components 21 1 Understanding the Differences between Controls and Components 213 Defi ning the Control Types 215 Building Derived Controls 216 Building UserControls 223 Building New Controls 225

xix

115962ftoc.indd5962ftoc.indd xixxix 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Building Components 227 Essential Considerations for Developing a New Component 228 Defi ning the New Component Project 229 Adding the New Component Code 230 Testing the Component 233 Testing Classes Using the Object Test Bench 235 Coding Your Application 239

Chapter 12: Coding the Application 24 1 Using an Appropriate Naming Convention 241 Using Hungarian Notation 242 Using Camel Case 242 Using Pascal Case 243 Adding Specialized Syntax 244 Adding Command Line Functionality 246 When to Use Command Line Switches 246 Adding Command Line Switches 248 Testing Command Line Functionality 253 Exiting the Application Properly 254 When Simply Closing the Form Isn’t Enough 254 Setting an Exit Code with System.Environment 255 Relying on Application.Exit() 258 Using P/Invoke to Exit Applications 259 Relying on Custom Features 261 Considering When to Use Custom Features 261 Using Custom Attribute and Exception Classes 262 Coding Your Application 262

Chapter 13: Considering the Documentation 26 5 Adding Documentation Support to Your Application 266 A View to a Document 266 Considering the Needs of the Developer 267 Considering the Needs of the User 268 Considering the Needs of the Administrator 269 Creating Documentation Comments 270 Considering the Kinds of Comments 270 Placing Comments in the Correct Place 273 Using the Resulting XML File 276

xx

115962ftoc.indd5962ftoc.indd xxxx 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Alternative Uses for Documentation Output 279 Using LINQ to Query the Documentation File 280 Creating Automated Application Reports from Documentation 280 Coding Your Application 280

Chapter 14: Implementing Error Handling 28 3 Discovering the Advantages of Assuming Nothing 284 Reviewing the Importance of Specifi c Error Handling 285 Using the Standard Exception Classes Correctly 286 Implementing Custom Error Handling 294 Considering the Importance of the Error Hierarchy 295 Providing Consistent Error Handling 296 Working with Single-Source Errors 297 Trapping Multiple-Source Errors 297 Finalizing Error Handling 297 Devising Self-Healing Applications 298 Coding Your Application 300

Chapter 15: Testing, Debugging, and Quality Assurance 30 3 Testing Your Application 304 Creating a New Test 304 Running Tests 308 Setting the Test Properties Using Test View 309 Organizing Tests Using the Test List Editor 311 Understanding the Code Coverage Results Window 312 Viewing the Test Runs 314 Considering the Human View of Testing 315 Debugging Your Application 316 Working with the Debugging Windows 316 Asking the IDE a Question 317 Viewing Memory Content 318 Obtaining Bugs from Users 319 Performing Quality Assurance Checks 319 Interacting with Team B 319 Constructing Useful Feedback 320 Getting the User Involved 321 Coding Your Application 322

xxi

115962ftoc.indd5962ftoc.indd xxixxi 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Chapter 16: Deploying the Application 32 3 Determining the Deployment Type 324 Considering the Testing Release 324 Considering the Local Deployment 325 Considering the Enterprise Deployment 326 Considering the Shrink-Wrap Deployment 327 Comparing Major and Minor Releases 328 Selecting a Deployment Method 330 Using the XCopy Deployment 330 Creating a Basic Setup Project 331 Modifying the Details 338 Using the Setup Project Wizard 339 Using the CAB Approach 340 Choosing a Media Type 342 Performing the Deployment 343 Evaluating the Results 344 Coding Your Application 345

Chapter 17: Supporting and Maintaining the Application 34 7 Creating Error Logs 347 Defi ning an Error 348 Handling Error Log Settings 349 Creating the Error Logging Class 349 Viewing the Error Log Output 352 Using Error Logs to Provide Support 353 Defi ning Errors, Sources, and Solutions 353 Determining the Need for Direct Developer Interaction 354 Creating Support Logs 354 Relying on Error Log and Support Logs for Maintenance 355 Prioritizing Maintenance Activities 355 Verifying Maintenance Requirements 355 Testing Maintenance Actions 356 Deploying Patches 356 Coding Your Application 356

xxii

115962ftoc.indd5962ftoc.indd xxiixxii 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Part III: Speed, Reliability, and Security 35 9

Chapter 18: Developing around the Performance Triangle 36 1 Acting after the Initial Development Phase 362 Defi ning the Performance Triangle 363 Characterizing the Speed Component 364 Characterizing the Reliability Component 365 Characterizing the Security Component 366 Creating the Perfect Triangle for Your Application 368 Making Your Application Reliable and Secure 369

Chapter 19: Considering Application Speed Implications 37 1 Defi ning the Trade-offs of Speed 372 Cutting Code in the Wrong Places 372 Creating Wrong Answers Really Fast 373 Measuring Speed 374 A Quick View of the Sample Code 375 Launching the Performance Wizard 376 Profi ling Performance 379 Viewing Performance Results 386 Comparing Performance Reports 389 Making Your Application Reliable and Secure 390

Chapter 20: Considering Application Reliability Implications 39 1 Defi ning the Trade-offs of Reliability 392 Avoiding the Overly Pessimistic Application 392 Creating Applications that Do Absolutely Nothing 392 Defi ning External Threats 394 Developing a Reliability Plan 395 Placing Users in Monkey Mode 395 Breaking Applications during Testing Is Good 396 Creating a Lessons-Learned Journal 397 Checking Your Code 398 Running Code Analysis 398 Calculating Code Metrics 400 Making Your Application Reliable and Secure 402

xxiii

115962ftoc.indd5962ftoc.indd xxiiixxiii 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Chapter 21: Considering Application Security Implications 40 3 Defi ning the Trade-offs of Security 404 Keeping Data out of the Hands of Users 404 Creating Performance Roadblocks 405 Considering Big Brother Syndrome 405 Breaking into Your Application 407 Relying on Internal Testers 407 Using a Third Party 409 Checking for the Impossible, Too 409 Understanding the Art of Interactions 410 Updating Your Security Profi le 411 Managing the Monitoring Process 412 Encrypting and Decrypting Data 413 Making Your Application Reliable and Secure 414 Part IV: Special Coding Methodologies 41 5

Chapter 22: Serializing XML 41 7 Using XML as a Storage Methodology 418 Implementing an XCopy Application 418 Viewing the Sample XML Data 419 Using XML Data in Reports 421 Creating the XML Storage Class 422 Defi ning the XML Data Elements 422 Creating the Customized Data Description 423 Designing Bulletproof Properties 424 Saving Data in XML Format 425 Choosing a Data Location 425 Implementing a Save Routine 426 Loading Data in XML Format 428 Obtaining the XML Data from Disk 428 Using XML Data to Modify the Environment 430 Manipulating Serialized XML Data 432 Adding New Data 432 Editing Existing Entries 433 Deleting Data Entries 436 Sorting the Data Entries 437 Displaying Data Entries on Screen 439 Implementing Your Design 439

xxiv

115962ftoc.indd5962ftoc.indd xxivxxiv 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Chapter 23: Working with LINQ 44 1 Understanding LINQ Support in Visual Studio 2008 442 Understanding LINQ and Providers 442 Contrasting LINQ with SQL 442 Using LINQ for Non-query Solutions 446 Defi ning the Basic Operators 446 Working with the From and Select Operators 447 Working with the Where Operator 447 Working with the orderBy Operator 448 Creating LINQ Queries 449 Defi ning Simple Queries 449 Performing Joins of Multiple Data Sources 450 Using the let Operator 453 Using LINQ to Interact with Any Data Source 454 Using the Built-In Providers 454 Using LINQ to Objects 455 Using LINQ to DataSet 457 Using LINQ to SQL 462 Using LINQ to XML 467 Using Third-Party Providers 472 Implementing Your Design 475

Chapter 24: Augmenting Applications Using F# 47 7 Understanding F# 478 Considering the Functional Language 478 Defi ning the Need for Functional Languages 479 Obtaining F# Support for Visual Studio 2008 479 Downloading the Product 480 Installing F# 480 Experimenting at the Command Line 481 Modifying the Command Line Path to Accommodate F# 481 Defi ning a Basic Script 482 Running the Application 482 Interacting with F# 483 Creating an F# Application 484 Implementing Your Design 486

Chapter 25: Creating Multithreaded Applications 48 7 Understanding Multithreading 488 Defi ning Multithreading 488 Considering the Benefi ts 490 xxv

115962ftoc.indd5962ftoc.indd xxvxxv 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Considering the Defi ciencies 491 Looking at the Current State of Multithreading 492 Creating a Basic Multithreaded Application 493 Defi ning the Project 494 Adding the File Processing Thread Class 494 Adding the Monitor Thread Class 495 Developing the Application Code 499 Testing the Multithreaded Application 502 Implementing Your Design 505

Chapter 26: Creating Reports and Other Output 50 7 Defi ning the Elements of a Good Report 508 Starting with the Acceptable Report 509 Improving the Acceptable Report to the Good Level 509 Testing the Report 510 Understanding User Report Requirements 511 Considering the Content Requirement 511 Defi ning Formatting Needs 512 Including Organizational Policies 512 Developing a User Rapport for Reports 513 Creating the Self-Modifying Report 514 Devising Other Forms of Application Output 515 Implementing Your Design 518

Chapter 27: Designing Low-Level Application Elements 51 9 Defi ning P/Invoke 519 Calling External Functions 521 Adding the Required Directives 522 Providing Structures, Enumerations, and Constants 522 Creating an External Function Reference 525 Working with Function Call Data 526 Using the External Function within Managed Code 527 Providing Error Handling 529 Designing Data Structures 534 Enumerating the Standard Console Handles 535 Creating the Basic Data Structures 535 Defi ning the Function Calls 537 Using Data Structures within Managed Code 538 Implementing Your Design 540

xxvi

115962ftoc.indd5962ftoc.indd xxvixxvi 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Part V: Resources and Tools 54 1

Chapter 28: Choosing Resources and Tools 54 3 Ending Time-Wasting Clutter 544 Creating the Archive 545 Updating Resources and Tools Effectively 547 Developing a Rapport with Vendors 547 Defi ning a Useful Resource 548 Defi ning a Useful Tool 549 Creating a Team Toolbox 550 Understanding the Team Needs 550 Defi ning Usage Requirements and Policies 551 Obtaining Resources and Tools 551

Chapter 29: Resources and Tools 55 3 Finding Microsoft Resources and Tools 554 Considering MSDN 554 Looking at TechNet 554 Interacting with Microsoft Download 555 Exploring Microsoft Research 556 Discovering Resources and Tools Online 557 Editing XML Files Using XML Notepad 2007 558 Obtaining XML Notepad 2007 559 Using XML Notepad 2007 the First Time 559 Making File Modifi cations 560 Using XSLT Files 560 Comparing XML Files 560 Enhancing Visual Studio 2008 Using Microsoft PowerCommands 561 Obtaining Resources and Tools 565

Chapter 30: Third-Party Resources and Tools 56 7 Finding Third-Party Resources and Tools 567 Using Vischeck to Test Application Colors 571 Using FxCop to Improve Code Reliability and Consistency 574 Obtaining a Copy of FxCop 575 Using the FxCop GUI 576 Repairing the Errors 577 Considering the Rules 580 Employing FxCop during a Build 581

xxvii

115962ftoc.indd5962ftoc.indd xxviixxvii 11/22/09/22/09 10:42:0910:42:09 AMAM Contents

Using LINQPad to Create LINQ Queries 582 Obtaining Resources and Tools 585

Glossary 587 Index 609

xxviii

115962ftoc.indd5962ftoc.indd xxviiixxviii 11/22/09/22/09 10:42:0910:42:09 AMAM