.NET
  Home arrow .NET arrow Page 8 - Introducing Code Generation
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Introducing Code Generation
By: Apress Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2004-12-08

    Table of Contents:
  • Introducing Code Generation
  • Generating a Simple Program
  • Creating the Template
  • Running the Template
  • Plain-Vanilla Code
  • Picking the Right Mechanism
  • Breaking Down the Code Generation Process
  • Writing Handcrafted Code
  • Tying It Together: Implementation and Testing
  • The Strongly Typed Dataset
  • Performing Real-World Code Generation
  • Generating a Simple Class via CodeDOM
  • Generating a Simple Class via XSLT Templates

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing Code Generation - Writing Handcrafted Code


    (Page 8 of 13 )

    Handcrafted code is code you still write line by line in the editor. Handcrafting code isn’t a new step in your development; it just places what you’re currently doing in a smaller and more focused context. You isolate the code you handcraft from the code built on templates so that regeneration doesn’t smash over your important handcrafted code. One of the key characteristics of code generation is respecting handcrafted code, and to do this you need to isolate and protect it.

    *****************

    7. Here I’m talking about very mechanical localized patterns. If you want to explore incorporating formally described patterns, I’ve included a couple of references in the “Additional Reading” section. It’s not essential to describe patterns in this formal sense to do application code generation because you’re often working with a pattern such as “that specific five lines of code repeated for every column,” which doesn’t really seem like working with formalized patterns. Code generation and patterns are closely linked.  

    There are several types of handcrafted code:

    • Startup
    • Exception reporting

    • Components

    • Utility methods

    • Base class functionality

    • Debug support

    • Code with insufficient, inconsistent, or interspersed patterns

    • Class-specific code

    In addition to the first six categories that are singleton in nature (meaning you only need one of them for your application), several characteristics in your target file might lead you to handcraft files that could also be candidates for generation. If the patterns are inconsistent and there are a number of complex variations, the logic of the template will reflect the complexity. If the pattern-based code is tightly interspersed with handcrafted code, it can be difficult to separate the handcrafted code. Finally, if there isn’t much of a repeated pattern, then it may be easier to handcraft.

    Handcrafted code has two key roles in your application—to provide a framework and to customize your application. The combination of metadata and handcrafted code give your application its unique personality, and the framework and templates provide its structure. Much of this customization resides in the class-specific code. Class-specific code includes all the ways one of your pat-tern-based classes differs from the others. For example, your Customers class may have a FullAlphaName property that isn’t present in any other class. Class-specific code accommodates both predictable variations such as validation rules and unpredictable changes such as additional properties. You’ll generally isolate class-specific code by placing it in derived classes.

    Application frameworks are also made of handcrafted code. Frameworks— the infrastructure of your application that you only need to write once—are interdependent with generated code. Generated code doesn’t implement the architecture you use for your current application by itself but needs handcrafted frameworks. Because they work together, you can move your frameworks and templates into your next application as a unit. Generated code needs base classes, a set of components, utility methods, and so on to complete the job of running your application.

    The goal of code generation is to build applications significantly faster and cheaper, so you want to introduce code generation techniques where they’ll have the highest payback first—generally middle-tier data containers and data access layers. As your skills and understanding of code generation increase, you’ll contemplate what other sections of your application might be good candidates to move from handcrafted code to autogenerated code. The amount of code that has insufficient or interspersed patterns decreases as your understanding increases. Initially your entire UI layer may fit into this category and remain handcrafted, but Chapters 9 and 10 describe strategies to maximize code generation in your UI layer.

    *******************

    TIP When the line between the code you should autogenerate and code you should handcraft seems blurry, lean to the conservative side and handcraft more code. Where templates are helpful, it’ll become obvious because you’ll notice you’re re-creating the same code, and you can add these additional templates later.

    Isolating your handcrafted code allows you to protect it across regeneration. Other than class-specific code, handcrafted code is naturally isolated from auto-generated code because it’s in different files that aren’t generated. It takes more care to isolate and protect class-specific code, as discussed in Chapter 4.

    ********************

    NOTE There are several ways to isolate class-specific handcrafted code. The easiest solutions in .NET rely on inheritance. You derive a class containing your class-specific code from an autogenerated base class. The most important single thing you’ll learn from this book may be to become comfortable with design decisions that allow you to combine code generation and class-specific code to provide each class full functionality and personality while retaining your ability to regenerate at will. 

    This chapter is from Code Generation in Microsoft .NET by Kathleen Dollard (Apress, 2004, ISBN: 1590591372). Check it out at your favorite bookstore today.

    Buy this book now.

    More .NET Articles
    More By Apress Publishing


     

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT