“Once this skill is within your everyday comfort zone, it will give your productivity a boost and increase your value to your team. I encourage you to add the techniques outlined in this book to your toolset and to use them to develop your own workbench of generative tools. In doing so, I’m confident you’ll improve your capabilities, and what’s more, have fun doing so.” __–From the Foreword by Gareth Jones, Developer Architect, Visual Studio__®__, Microsoft__ **Enlist Visual Studio’s Built-in Code Generation Tools to Write Better Software Faster** Automatic code generation can dramatically increase your productivity, improve code quality and maintainability, promote reuse, and help you extend best practices throughout your development organization. .NET and Visual Studio contain many powerful, code-generation tools--and this book shows you how to succeed with all of them. With **__**Practical Code Generation in .NET**__**, spend less time writing monotonous, repetitive code--leaving more time to address troublesome areas! Microsoft MVP Peter Vogel covers code generation with Visual Studio 2010, 2008, and 2005, as well as all recent versions of .NET, including .NET 4.0. You’ll learn when enlisting code generation makes sense and how to design solutions that build on the skills and resources you already have. Writing for experienced programmers, Vogel shows how to generate reliable code using procedural code, Visual Studio add-ins, XML, configuration files, and more--including Microsoft’s innovative CodeDOM technology for generating code in multiple languages. He brings everything together in three complete, chapter-length case studies. Coverage includes • Understanding the structure of .NET code-generation solutions and best practices for architecting them • Creating Visual Studio add-ins that quickly integrate code generation into day-to-day activities • Using objects and methods to add or remove project components • Using text insertion to generate code using any tool--even standard string handling functions • Working with the specific features of C#, Visual Basic .NET, and ASP.NET • Generating more concise code with .NET’s new Text Template Transformation Toolkit (T4) • Building code-generation solutions with Visual Studio templates, attributes, and custom tools • Distributing code-generation solutions Contents 5 Foreword 15 Preface 17 Acknowledgments 21 About the Author 22 Chapter 1 Introducing Code Generation 24 Repetitive Code: Your History 25 Copy-and-Paste 25 General-Purpose Code 26 Benefits of Code Generation 27 When to Use Code Generation 30 Best Practices in Code-Generation Solutions 33 Code-Generation Process 34 Code-Generation Tools 35 Model-Driven Architecture, Declarative Programming, and Code Generation 37 Model-Driven Architecture 37 Declarative Programming 40 PART I: TOOLS 42 Chapter 2 Integrating with Visual Studio 44 Design-Time Integration 45 Starting Your Visual Studio Add-In 46 Creating a Menu-Driven User Interface 59 Working with Visual Studio Windows 72 Responding to Events 81 Accepting Input 88 Integrating with Visual Studio 99 Chapter 3 Manipulating Project Components 100 The Code Model 101 Managing Projects and Solutions 103 Generating Code 109 Modifying and Analyzing Components 133 Processing Components in a File 137 Working with Retrieved Elements 141 Working with the CodeType Objects 144 Storing Information 149 Working with Project Components 151 Chapter 4 Modifying Code in the Editor 152 Opening and Closing Documents and TextDocuments 153 Backing Out Changes 156 Creating EditPoints to Access Text 158 Retrieving Information 162 Accessing Text with an EditPoint 163 Working with Bookmarks 170 Inserting, Replacing, and Formatting Text 172 Inserting and Reading Text 178 Chapter 5 Supporting Project-Specific Features 180 Introducing the VSLangProj Libraries 181 Managing a “Projectless” Website 193 Project-Specific Features 203 Chapter 6 Generating Language-Neutral Code 204 A Comprehensive Example 205 Declarations 212 Defining Classes 220 Class Members 223 Statements and Expressions 239 Code Structures 251 When All Else Fails: Code Snippets 258 Other Code Features 261 Code Providers 266 Generating Code 270 Chapter 7 Generating Code from Templates with T4 272 T4 in Visual Studio 274 T4 Code-Generation Strategies 275 Creating a T4 Template 277 Extending T4 288 Invoking Templates from Code 293 Leveraging Templates 306 Chapter 8 Other Tools: Templates, Attributes, and Custom Tools 308 Item Templates 309 Attributes 328 Generating Code from Custom File Formats 338 Reviewing the Tools 352 PART II: CASE STUDIES 354 Chapter 9 Case Study: Generating a Connection String Manager 356 Defining the Problem 357 Setting Up the Add-In 360 Creating the Code Generator 366 Customizing the Template 373 Generating Code 378 Reading Input 379 Notifying the Developer 382 Supporting Customization 385 Tying Generation to Events 394 Generating a Simple Class 398 Chapter 10 Case Study: Generating Validation Code 400 Defining the Problem 401 Starting the Code-Generation Project 411 Wiring Up the Add-In 412 Starting the Generation Utility 418 Defining a Template 420 Generating Code 423 Specifying Code with the CodeDom 434 Generating Code in the Target Language 444 Supporting “Projectless” Websites 444 Handling Errors 448 Building a Complete Code-Generation Solution 449 Chapter 11 Case Study: Generating Data-Conversion Code 450 Defining the Problem 451 Creating the Designer 455 Enhancing Model Validation 473 Generating Code 477 Distributing Your Designer 481 Visual Studio 2010 Additions 488 Capturing Inputs 489 PART III: APPENDIXES 490 Appendix A: Generating Menu Names 491 Appendix B: Options Dialog Categories, Subcategories, and Properties 492 Appendix C: A Code-Generation Add-In 493 Integrating Code-Generation Classes 493 Appendix D: Distributing Code-Generation Solutions 504 Creating the .vscontent File 504 A Complete Example 508 Installing the Solution 509 Index 510 A 510 B 511 C 511 D 515 E 516 F 517 G 518 H 518 I 519 J-K-L 520 M 520 N 520 O 521 P 521 Q-R 522 S 523 T 524 U 525 V 525 W-X-Y-Z 527
“Once this skill is within your everyday comfort zone, it will give your productivity a boost and increase your value to your team. I encourage you to add the techniques outlined in this book to your toolset and to use them to develop your own workbench of generative tools. In doing so, I’m confident you’ll improve your capabilities, and what’s more, have fun doing so.”
–From the Foreword by Gareth Jones, Developer Architect, Visual Studio®, Microsoft
Enlist Visual Studio’s Built-in Code Generation Tools to Write Better Software Faster
Automatic code generation can dramatically increase your productivity, improve code quality and maintainability, promote reuse, and help you extend best practices throughout your development organization. .NET and Visual Studio contain many powerful, code-generation tools—and this book shows you how to succeed with all of them. With Practical Code Generation in .NET, spend less time writing monotonous, repetitive code—leaving more time to address troublesome areas!
Microsoft MVP Peter Vogel covers code generation with Visual Studio 2010, 2008, and 2005, as well as all recent versions of .NET, including .NET 4.0. You’ll learn when enlisting code generation makes sense and how to design solutions that build on the skills and resources you already have.
Writing for experienced programmers, Vogel shows how to generate reliable code using procedural code, Visual Studio add-ins,
Coverage includes
• Understanding the structure of .NET code-generation solutions and best practices for architecting them
• Creating Visual Studio add-ins that quickly integrate code generation into day-to-day activities
• Using objects and methods to add or remove project components
• Using text insertion to generate code using any tool—even standard string handling functions
• Working with the specific features of C#, Visual Basic .NET, and ASP.NET
• Generating more concise code with .NET’s new Text Template Transformation Toolkit (T4)
• Building code-generation solutions with Visual Studio templates, attributes, and custom tools
• Distributing code-generation solutions