practical code generation in .net

529 432 0
practical code generation in .net

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

www.it-ebooks.info P RACTICAL C ODE G ENERATION IN .NET C OVERING V ISUAL S TUDIO ® 2005, 2008, AND 2010 Peter Vogel Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Capetown • Sydney • Tokyo • Singapore • Mexico City www.it-ebooks.info Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trade- marks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for inciden- tal or consequential damages in connection with or arising out of the use of the information or programs con- tained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States please contact: International Sales international@pearson.com Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data: Vogel, Peter, 1953- Practical code generation in .NET : covering Visual Studio 2005, 2008, and 2010 / Peter Vogel. p. cm. Includes bibliographical references and index. ISBN 978-0-321-60678-5 (pbk. : alk. paper) 1. Microsoft Visual studio. 2. Code generators. 3. Microsoft .NET Framework. I. Title. QA76.76.G46V65 2010 006.7'882 dc22 2010003301 Copyright © 2010 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or like- wise. For information regarding permissions, write to: Pearson Education, Inc. Rights and Contracts Department 501 Boylston Street, Suite 900 Boston, MA 02116 Fax (617) 671 3447 ISBN-13: 978-0-321-60678-5 ISBN-10: 0-321-60678-7 Text printed in the United States on recycled paper at RR Donnelly in Crawfordsville, Indiana. First printing April 2010 www.it-ebooks.info For Jan. Always. www.it-ebooks.info iv C ONTENTS Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xx About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Chapter 1 Introducing Code Generation . . . . . . . . . . . . . . . . . . . . . 1 Repetitive Code: Your History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Copy-and-Paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 General-Purpose Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Benefits of Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 When to Use Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Best Practices in Code-Generation Solutions . . . . . . . . . . . . . . . . . . . 10 Code-Generation Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Code-Generation Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Model-Driven Architecture, Declarative Programming, and Code Generation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Model-Driven Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Declarative Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 PART I: T OOLS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Chapter 2 Integrating with Visual Studio . . . . . . . . . . . . . . . . . . . . 21 Design-Time Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Starting Your Visual Studio Add-In . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Laying the Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Integrating with Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Writing a Generation Class in Visual Studio . . . . . . . . . . . . . . . . . 30 Working with COM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 www.it-ebooks.info Contents v Debugging Issues in Visual Studio 2005/2008 . . . . . . . . . . . . . . . 32 The OnConnection Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Removing Add-In Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Creating a Menu-Driven User Interface . . . . . . . . . . . . . . . . . . . . . . . 36 Extending Your Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Adding Additional Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Finding the Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Adding the Menu Item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 Supporting Multiple Menu Items . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Creating Submenus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 Adding Submenus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Context Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Accessing the Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Working with Visual Studio Windows . . . . . . . . . . . . . . . . . . . . . . . . 49 Using the Task List Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 Retrieving Selected Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Writing Messages to the Output Window and the TaskList . . . . . . . 51 AutoNavigate in Visual Studio 2005 . . . . . . . . . . . . . . . . . . . . . . 55 Final Touches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Responding to Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Simple Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Filtered Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 Working with Document Events . . . . . . . . . . . . . . . . . . . . . . . . . . 61 Extracting Event Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Finishing Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 Accepting Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Creating a Dockable Window . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Saving Input Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 Adding an Options Tab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Accessing and Saving Option Properties . . . . . . . . . . . . . . . . . . . . 73 Integrating with Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Chapter 3 Manipulating Project Components . . . . . . . . . . . . . . . . . 77 The Code Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Code Model Caveats . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 www.it-ebooks.info Managing Projects and Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Creating Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Referencing the Project Items Collection . . . . . . . . . . . . . . . . . . . . 82 Adding Folders to a Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Adding Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 Copying Boilerplate Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Generating Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Introducing CodeModel and FileCodeModel . . . . . . . . . . . . . . . . . 86 Generating Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Modifying and Analyzing Components . . . . . . . . . . . . . . . . . . . . . . 110 Finding Projects and Project Items . . . . . . . . . . . . . . . . . . . . . . . . 110 Reading Project Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Removing Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Checking for Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Processing Components in a File . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Processing All the Components in a File . . . . . . . . . . . . . . . . . . . 114 Retrieving Components by Name . . . . . . . . . . . . . . . . . . . . . . . . 116 Retrieving Components by Location . . . . . . . . . . . . . . . . . . . . . . 117 Working with Retrieved Elements . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Determining If an Element Can Be Modified . . . . . . . . . . . . . . . . 118 Writing Language-Specific Code . . . . . . . . . . . . . . . . . . . . . . . . 119 Using CodeElement with the Position Parameter . . . . . . . . . . . . . . 119 Choosing Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 The CodeEvent Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Working with the CodeType Objects . . . . . . . . . . . . . . . . . . . . . . . . 121 Finding Components with CodeType . . . . . . . . . . . . . . . . . . . . . . 121 Simple CodeType Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Working with Comments and Documentation Comments . . . . . . . . 124 Working with Related Classes . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Storing Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Storing Strings in the Globals Object . . . . . . . . . . . . . . . . . . . . . 127 Working with Project Components . . . . . . . . . . . . . . . . . . . . . . . . . 128 Chapter 4 Modifying Code in the Editor . . . . . . . . . . . . . . . . . . . . 129 Opening and Closing Documents and TextDocuments . . . . . . . . . . . . 130 Accessing the Document/TextDocument . . . . . . . . . . . . . . . . . . . . 130 Making the Document Available to the Developer . . . . . . . . . . . . 131 Closing the Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 vi Contents www.it-ebooks.info Contents vii Backing Out Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Creating and Finishing a Context . . . . . . . . . . . . . . . . . . . . . . . . 133 Getting Information on the Context . . . . . . . . . . . . . . . . . . . . . . . 135 Creating EditPoints to Access Text . . . . . . . . . . . . . . . . . . . . . . . . . . 135 Retrieving EditPoints with the TextDocument . . . . . . . . . . . . . . . . . 136 Retrieving EditPoints with the FileCodeModel . . . . . . . . . . . . . . . . 136 Retrieving CodeElements from Text . . . . . . . . . . . . . . . . . . . . . . . 138 Retrieving Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .139 Document and TextDocument Objects . . . . . . . . . . . . . . . . . . . . . 139 Document Object Only . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 EditPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Accessing Text with an EditPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Retrieving Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 Relocating the EditPoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Finding Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Finding Text with Regular Expressions . . . . . . . . . . . . . . . . . . . . . 146 Working with Bookmarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Creating Bookmarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Inserting, Replacing, and Formatting Text . . . . . . . . . . . . . . . . . . . . 149 Inserting and Deleting Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 Replace Selected Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 Bulk Insertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Cleaning Up after Insertions . . . . . . . . . . . . . . . . . . . . . . . . . . . 151 Bulk Replacements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152 Controlling the Text Being Displayed . . . . . . . . . . . . . . . . . . . . . . 153 Formatting Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Smart Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Controlled Indenting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 Inserting and Reading Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Chapter 5 Supporting Project-Specific Features . . . . . . . . . . . . . . 157 Introducing the VSLangProj Libraries . . . . . . . . . . . . . . . . . . . . . . . . 158 Working with Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Reference Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 Managing a “Projectless” Website . . . . . . . . . . . . . . . . . . . . . . . . . 170 Working with the Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 Adding New Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 Working with Website Items . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 Project-Specific Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 www.it-ebooks.info Chapter 6 Generating Language-Neutral Code . . . . . . . . . . . . . . 181 A Comprehensive Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 The Code to Be Generated . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 Using Variables, Data Types, and Literals . . . . . . . . . . . . . . . . . . 184 Creating Namespaces and Classes . . . . . . . . . . . . . . . . . . . . . . 185 Adding Methods with Parameters . . . . . . . . . . . . . . . . . . . . . . . . 186 Adding Statements to Methods . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Generating Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Local Scalar Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 Delegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Defining Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Inheritance and Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198 Generics and Partial Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 Class Members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 Entry Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 Creating an Indexer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Statements and Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Code Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 If Then . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 Try Catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 When All Else Fails: Code Snippets . . . . . . . . . . . . . . . . . . . . . . . . 235 Snippet Expressions and Statements . . . . . . . . . . . . . . . . . . . . . . 236 Compile Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Other Code Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 Generating Valid Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 viii Contents www.it-ebooks.info Contents ix Adding Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 Adding Custom Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 Using Directives to Organize Code into Regions . . . . . . . . . . . . . 241 UserData (Option Strict and Option Compare) . . . . . . . . . . . . . . 242 Code Providers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Generating Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 Generating Partial Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Compiling Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 Generating Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Chapter 7 Generating Code from Templates with T4 . . . . . . . . . . 249 T4 in Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 T4 Code-Generation Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Options for Extending T4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 The T4 Process . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Creating a T4 Template . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Escape Characters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Adding Helper Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 Generating Errors and Warnings . . . . . . . . . . . . . . . . . . . . . . . . 263 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Accessing the Generated Code . . . . . . . . . . . . . . . . . . . . . . . . . 264 Controlling Code Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 Extending T4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Creating a New Base Class . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 Defining Custom Directives . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267 Invoking Templates from Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Configuring the Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 Invoking the Template Using the Visual Studio Host . . . . . . . . . . . 272 Invoking the Template with a Custom Host . . . . . . . . . . . . . . . . . . 273 Defining a Custom Host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274 Adding Custom Methods to the Host . . . . . . . . . . . . . . . . . . . . . . 280 Passing Parameters to a Template . . . . . . . . . . . . . . . . . . . . . . . . 281 Supporting Custom Directives . . . . . . . . . . . . . . . . . . . . . . . . . . 282 Leveraging Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 www.it-ebooks.info [...]... lives in Goderich, Ontario, Canada—officially, the “prettiest town in Canada.” www.it-ebooks.info C H A P T E R 1 INTRODUCING CODE GENERATION In this chapter: • • • • • • • Repetitive Code: Your History Benefits of Code Generation When to Use Code Generation Best Practices in Code- Generation Solutions Code- Generation Process Code- Generation Tools Model-Driven Architecture, Declarative Programming, and Code. .. Responding to the Add -In Defining a Template Generating Code Starting the Process Managing the Code- Generation Process Utility Methods Processing the Validators Inserting Code Specifying Code with the CodeDom Generating the Lookup Methods Generating Code in the... losing any of their own code To support this, any code www.it-ebooks.info Code- Generation Process 11 that the developer writes should be kept separate from generated code ideally, the two kinds of code should be kept in separate files Rather than modifying code in the developer’s files, code- generation solutions should generate new files containing new classes (or interfaces, etc.) By keeping generated code. .. business objects that need the ability to delete records in a database There is a wide variety of scenarios that the code in the objects will need to be able to handle, including the following: ■ ■ ■ ■ Deleting a single row in a table Deleting multiple rows in a single table Deleting multiple rows in multiple tables Copying records to an archive table before removing them from their original table In. .. generation into your application development toolkit: ■ Improved productivity is the most obvious reason for using code generation Provided that creating the inputs for a code- generation www.it-ebooks.info 6 Chapter 1 Introducing Code Generation ■ ■ ■ ■ ■ solution takes less time than writing the code, code generation makes you more productive Reliability is improved through the use of code generation. .. one code generation to another The third step in the code- generation process is to add that common code You can either generate this code or use a Visual Studio template as the starting point www.it-ebooks.info 12 Chapter 1 Introducing Code Generation 4 Customize After the base code is created, the second-to-last step is to generate the code that varies from one code generation to another and then insert... Appendix C A Code- Generation Add -In 470 Integrating Code- Generation Classes Adding the Submenu Button Responding to Events Responding to the Menu Button An Extensible Solution 470 471 475 479 479 Appendix D Distributing Code- Generation Solutions 481 Creating the vscontent File Adding Add-Ins Adding Templates Adding a... Options Dialog Saving Developer Choices Option Manager Class Creating the User Control Implementing the User Control Interface Integrating with the Add -In Generating Custom Code Adding Custom Code Tying Generation to Events Integrating with Builds Integrating with Documents Generating a Simple Class ... Generating a Connection String Manager 333 Defining the Problem A Model Solution Supporting Customization Setting Up the Add -In Defining the Add -In Creating the Menu Calling the Solution Creating the Code Generator Finding the Project Does Anything Need to be Done? Segregating Generated Code. .. instance) But just because code generation eliminates the problems associated with the alternatives, that doesn’t necessarily mean you should you invest your time in implementing code- generation solutions You could, after all, still type in the necessary code However, in addition to avoiding the problems associated with the other solutions, there are several positive benefits to incorporating code generation . contact: International Sales international@pearson.com Visit us on the Web: informit.com/aw Library of Congress Cataloging -in- Publication Data: Vogel, Peter, 1953- Practical code generation in .NET. solutions. www.it-ebooks.info ■ Chapter 2, “Integrating with Visual Studio,” gives you enough infor- mation about creating Visual Studio add-ins for you to integrate code generation into your standard. tools you need to insert text into files. This allows you to generate code using any tool you want (even standard string-handling functions) and then insert that code into a file in your project. ■

Ngày đăng: 24/04/2014, 15:45

Từ khóa liên quan

Mục lục

  • Contents

  • Foreword

  • Preface

  • Acknowledgments

  • About the Author

  • Chapter 1 Introducing Code Generation

    • Repetitive Code: Your History

      • Copy-and-Paste

      • General-Purpose Code

      • Benefits of Code Generation

      • When to Use Code Generation

      • Best Practices in Code-Generation Solutions

      • Code-Generation Process

      • Code-Generation Tools

      • Model-Driven Architecture, Declarative Programming, and Code Generation

        • Model-Driven Architecture

        • Declarative Programming

        • PART I: TOOLS

          • Chapter 2 Integrating with Visual Studio

            • Design-Time Integration

            • Starting Your Visual Studio Add-In

            • Creating a Menu-Driven User Interface

            • Working with Visual Studio Windows

            • Responding to Events

            • Accepting Input

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan