.NET
  Home arrow .NET arrow Page 2 - 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 
Mobile Linux 
App Generation ROI 
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 - Generating a Simple Program


    (Page 2 of 13 )

    Looking at a “Hello World” program in each of the three primary code generation mechanisms lets you focus on the similarities and differences in the mechanisms themselves. Listing 1-1 shows the target “Hello World” program.

    Listing 1-1. A Target “Hello World” Program for a Preliminary Look at Code Generation

    Option Strict On
    Option Explicit On

    Imports System

    ' Class Summary: Hello World target output

    Public Class TargetHelloWorld

    #Region "Public Methods and Properties"
       Public Shared Sub Main()
          Console.WriteLine("Hello World")
        End Sub
    #End Region

    End Class

    In the real world, this isn’t a good candidate for code generation because it isn’t a pattern applied with predictable variations. It’s always the same, so it’s easier to write this “Hello World” program without code generation than with it. But the goal here is to strip code generation down to its naked essentials to illustrate the underlying process.

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

    NOTE The code you can download for this book (in C# and VB .NET) will include the best practices I use. In VB .NET I always use Option Strict On and Option Explicit On for strict typing. In VB .NET, I include Imports System and limit other imported namespaces to leave part of the namespace in each class reference. Imports System provides access to the system data type names, which I prefer for clarity. I also heavily use regions. Regions make it easier to trace between the output code and the emitting code while you’re debugging your templates. I’ll rarely use the VB library, which, among other things, makes it easier for C# developers to read my code. I’ll avoid setting options or imports at the project level, preferring that each file be explicit and self-contained. These details aren’t always evident in the code fragments printed in the book. You can assume that all VB .NET code is strictly typed (Option Strict and Option Explicit both set to On).

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

    TIP Specify code options in generated code, and don’t rely on project settings. You can’t be sure of what project setting will be in use in the generated application.

    Generating “Hello World” via Brute Force

    The obvious approach to code generation is to create a stream writer and start outputting code. The big benefit of this method is that it requires only a basic understanding of writers and streams, which you may already have. The vast majority of programmers I’ve talked to who have experimented with code generation have used this approach. Later you’ll see one way to organize your templates to make this option easier. Brute-force code generation doesn’t impose the same level of organization on you that XSLT code generation does, but with some discipline you can manually supply this same high level of organization, and it’ll benefit your code generation process.  

    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