Code Generation in Microsoft .NET

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Code generation has the potential to revolutionize application development. Rather than handcrafting each piece of code, developers are increasingly turning to code generation based on templates and applications of business logic to automatically perform a variety of tasks. This book teaches the technical details of code generation in .NET through a coherent series of steps that will help you to incorporate code generation into your own development efforts.

Veteran author Kathleen Dollard teaches code generation as a scripted repeatable process using templates you control, so you're not tied to a particular framework or style. Because you can regenerate code at any time, you can incorporate changes, including database changes, throughout the life of your application. The templates are flexible and designed to work smoothly with the handcrafted code youll use to customize your application. The underlying fundamentals are explained along with three specific techniques: outputting code to a stream, using the Code DOM, and using XSLT-based code generation.

In addition to the text, the tools in the book (downloadable in both VB .NET and C#) include a mechanism to extract information from SQL Server; a tool for editing and running code-generation scripts; a simple, flexible ORM tool that relates your database structure to your runtime class model; and a set of templates you can use as the starting point for your adventures in code generation.

Generating repetitive sections of code frees you to focus on the features that make your application unique. Code generation will turbo-charge your development cycles by offering speed, reusability, agility, and consistency. Go forth and generate!

Author(s): Kathleen Dollard
Edition: 1
Publisher: Apress
Year: 2004

Language: English
Pages: 761

Table of Contents

Cover

Code Generation in Microsoft .NET

ISBN 1590591372

Contents at a Glance

Contents

Foreword by Rockford Lhotka

Foreword by Don Kiely

About the Author

About the Technical Reviewer

Acknowledgments

Introduction

How Will Code Generation Help You?
Traveling Down this Path
A Note on Language and Tools
What Does This Book Cover?

Part One - The Process

Introducing Code Generation
Exploring Mechanisms for Code Generation
Generating a Simple Program
o Creating the Template
o Running the Template
o Creating the Template
o Understanding the Benefits of XSLT Code Generation
o Running the Template
o Creating the Template
Picking the Right Mechanism
Breaking Down the Code Generation Process
o Designing Architecture
o Collecting Metadata
o Outputting Code
o Writing Handcrafted Code
o Tying It Together: Implementation and Testing
o You Have Control Over the Template
o Metadata Is a Distinct Step
o Implement One-Click Regeneration
o Embrace Handcrafted Code
o Generated Code Has Great Quality, Performance, and Maintainability
Performing Real-World Code Generation
Summary
Additional Reading
Extracting Metadata
Introducing the Process
o Using Schema Definitions Such As XSD
o Extracting Metadata from Databases Such As SQL Server
o Entering Metadata Manually
o Extracting Metadata from Web Services via WSDL
o Retrieving Metadata from External Sources
o Extracting Metadata from Design Tools Such As UML
o Extracting Metadata from Existing Applications and Source Code
Understanding XSD's Role in Code Generation
Working with SQL-92 Databases (SQL Server)
o Understanding the Target Structure
o Understanding the Tool Architecture
o Understanding Security
o Understanding SQL-92 Terminology
o Introducing the Specific Schemas
o Using Constraints
o Modifying Mappings
o Determining Stored Procedure Intent
o Retrieving Stored Procedure Recordsets
o Retrieving Identity Columns
o Retrieving Extended Properties
o Implementing Naming Delimiters
o Implementing Naming Rules
Creating Freeform Metadata
o Using Skip Attributes
o Using an XSD
Merging Metadata
Summary
Additional Reading
Outputting Code
Understanding the Elements of the Code Generation Harness
o Working with Abstract File Paths
o Providing Parameters
Examining Code Generation Mechanics
o Creating a Class
o Capping Off XSLT Code Generation
o Using the IndentTextWriter
o Creating a Class
o Winding Up Brute-Force Code Generation
o Using an Object Hierarchy
o Building a CodeDOM Graph
o Exploring Other Features
o Testing in All Target Languages
o Working with the CodeDOM
Summary
Additional Reading
Writing Handcrafted Code
Effectively Using Handcrafted Code
o Running Startup Code
o Incorporating Exception Reporting
o Using Components
o Using Utility Methods
o Using Code with Insufficient or Interspersed Patterns
o Providing Reusable Functionality with a Base Class
o Providing Class-Specific Code Functionality with a Derived Class
o Providing Debug Support
Exploring Mechanisms for Isolating Handcrafted Code
o Exploring Object-Oriented Fundamentals
o Instantiating the Most Derived Class
o Looking at an Icky, Tricky OOP Detail
o Using Validation as an Example of Overriding Behavior
o Using Derived Methods to Extend Behavior
o Using Events to Provide Behavior
Summary
Tying It All Together
Getting Into the Harness Details
o Introducing the Standard Element
o Introducing the Process Element
o Introducing the XSLTFiles Element
o Introducing the SinglePass Element
o Introducing the MultiPass Child Element
o Introducing the OutputRules Element
o Introducing the Parameter Element
o Using the XSLTProcess Directive
o Using the RunProcess Directive
o Using the CreateMetadata Directive
o Using the MergeMetadata Directive
o Using the XSLTGeneration Directive
o Using the BruteForceGeneration Directive
o Using the CodeDOMGeneration Directive
o Using the CopyFiles Directive
o Using the RunSQLScripts Directive
o Using the Nested Scripts Directive
Organizing Files via the Directory Structure
Building Project Files
o Directing the Process
o Building the FileList
o Building the File List XML
o Providing Overrides
o Running the Template
Incorporating Source Control
o Initiating Source Control Tasks
o Accessing the SourceSafe Wrapper Object
Debugging
o Debugging Generated Output
o Debugging Metadata
o Debugging Harness Directives
o Debugging Generation Harness
o Debugging Templates
Managing the Project over the Long Haul
o Understanding the Create Solution Tool
o Calling the Create Solution Tool
Summary
Additional Reading

Part Two - Code Generation in Action

Mapping Database Structure into Business Objects
Creating the Object Relation Mapping (ORM) Solution
o Simplifying ORM Mapping
o Automating the Flow of Database Structure Changes
o Creating Code Generation-Friendly Metadata
o Describing Complex Stored Procedures
o Controlling Metadata Visibility
o Overriding Inferred Details for Customization
o Setting Characteristics of Generated Code via ORM Flags
o Managing Responsibility
o Specifying the Simplest ORM Definition
o Specifying Minimal Stored Procedure Information
o Limiting Children: Avoiding the Mega-Object Problem
o Specifying Individual Properties
o Defining Special Types
o Applying Different Stored Procedure Privileges
o Generating Joined Tables
o Specifying Handcrafted Stored Procedures
o Specifying the MappingRoot
o Setting Default Privileges
o Specifying Assembly Details
Morphing ORM into Generation-Friendly Metadata
o Step 1: Creating Metadata
o Step 2: Merging Metadata
o Introducing Base Pattern Used in Steps 3-8
o Step 3: Processing ORMObjectOne.xslt
o Step 4: Processing ORMObjectTwo.xslt
o Step 5: Processing ORMBuildOne.xslt
o Step 6: Processing ORMBuildTwo.xslt
o Step 7: Processing IncorporateInfo.xslt
o Step 8: Processing ORMFilterForGen.xslt
Summary
Generating Stored Procedures
Exploring Stored Procedure Generation
Generating Retrieve Stored Procedures
o Opening the Stored Procedure with the OpenSP Template
o Closing the Stored Procedure with the CloseSP Template
o Outputting Parameters with the SPParameters Template
o Outputting Columns with the ColumnSPList Template
o Looking at the Three Templates to Join Tables
Generating Create Stored Procedures
Generating Delete Stored Procedures
Generating Update Stored Procedures
Generating SetSelect Stored Procedures
Summary
Additional Reading
Generating Middle Tier Templates
Introducing Component-based, Scalable, Logical Architecture (CSLA)
Changing the Base Architecture
Generating the Middle Tier
o Opening Declarations and Entry-Level Template
o Processing the High-Level Template
o Declaring the Class
o Outputting the Template for Business Properties and Methods
o Outputting Property Procedures
o Comparing Values
o Checking Privileges
Summary
Additional Reading
Generating a WinForms Application
Reviewing WinForms Architecture
Developing Code Generation Strategies for WinForms
o Merging Additional Metadata
o Transforming Metadata for Easy UI Generation
o Adding Manual Metadata for UI Generation
o Implementing Dynamic Menus
o Working with the Generic EditForm
o Using Flow Layout in WinForms
o Handling Primary Keys
Exploring User Interface Templates
o Building the Edit Layout Template
o Building the Generated UserControl Template
Summary
Generating a Web User Interface
Reviewing ASP.NET
o Looking at the Anatomy of a Postback
o Improving Performance
o Allowing Navigation
Exploring Code Generation Strategies for ASP.NET
o Avoiding Redundant Declarations
o Locating and Naming Code-Behind Files
o Abstracting URLs to a Common Class
o Using the Abstracted URLs
o Building the URL Abstraction Classes
Walking Through ASP.NET Templates
Summary
Additional Reading
Establishing Perspective
Selling Code Generation to Management
Investigating Upcoming Features
o Storing .NET Types
o Running .NET Code
o Using XML Data and XQuery
o Exploring Code Snippets
o Refactoring
o Looking at Edit and Continue
o Exploring Generics
o Splitting Code with Partial Classes
o Using Flow Layout for WinForms
Envisioning the Future
o Sharing Templates and Frameworks
o Standardizing Metadata: The Easy Part
o Standardizing Runtime Application Interfaces: The Hard Part
o Providing Tools for Each Step
o Marking Generated Code Files
o Supporting a Standard Hash Mechanism
o Providing an XSLT Jump to Template at Design Time
o Supplying a Template-Based Strongly Typed DataSet
o Enhancing the CodeDOM
o Refactoring Code and/or Snippets to Templates
Building New Paradigms
Going Forward
Summary

Appendix A: X-Survival

Starting the XML Basic Training
Introducing Elements
Introducing Attributes
Introducing XML Rules
Handling Special Characters
Introducing Whitespace
Working with Minimal Feedback
Surviving Namespaces
Dealing with the XSD Drill Sergeant
Understanding Key XSD Nodes
Introducing Cardinality
Introducing Compositors
Validating XML in .NET
Using the Target Namespace
Editing X-Files in Visual Studio
The Commando's Guide to XSLT
Using Named Templates
Using Match Templates
Passing Parameters
Executing Conditionally with xsl:if
Branching with xsl:choose
Looping with xsl:for-each
Understanding Context
Outputting Literals
Outputting Tokens with xsl:value-of
XPath Marks the Spot
Understanding Context
Using Location Paths
XSLT and XPath Reference
Additional Reading

Appendix B: Dissecting the Metadata Extraction Tools

Converting XSD into Friendly XML
Extracting Metadata from Databases
Exploring the Class Opening
Declaring MustOverride Methods
Implementing CreateMetadata
Looping Through All Tables
Outputting XML for Each Table
Outputting XML for Each Column
Outputting XML for Column Privileges
Outputting Check Constraints
Outputting Table Constraints
Outputting Primary Key Information
Outputting Relations
Looking Back at the ExtractMetadata Class
Exploring the Constructor
Getting a Connection
Implementing MustOverride Methods
Supporting Extended Properties
Ignoring Specific Tables
Looking Back at the SQLExtractMetadata Class

Appendix C: Details of the Code Generation Harness

Building a Dynamic Data-Driven Application
Loading the XML
Popping Up the Add Menu
Adding Elements
Making a Generic Node
Displaying an Element
Executing the Script
Looking Back at the Generation Harness

Appendix D: CodeDOM Issues, Tips, and Tricks

Extending the CodeDOM
Looking at Broad Issues with the CodeDOM
Exploring Language-Specific Details
Testing in All Target Languages

Index