Introducing Code Generation - Picking the Right Mechanism
(Page 6 of 13 )
To integrate any of these three mechanisms into your code generation process, you’ll separate your application into code you’ll generate and code you’ll write by hand the old way, line by line in an editor. I call the manually written code handcrafted code because it brings to mind the respect I have for handcrafted items. If I build something in my wood shop, I take joy and pride in the crafts I shape by hand. But if I built furniture without power tools, I’d probably never finish, or the quality of the piece would suffer when I rushed. I simply can’t rip5 an 8-foot oak 2×4 efficiently with a handsaw.
Code is the same way. Some jobs are like ripping that long board. Doing them by hand is tedious, demands careful attention to detail, and requires significant skill to maintain the desired consistency. If you decide not to rip the board by hand and create the programmer’s version of a table saw via code generation, you’re creating the right tool for the job. That leaves you more time to focus on parts of the job that aren’t appropriate for the table saw—or creating the parts of your application that aren’t appropriate for code generation.
If you go into a well-stocked building supply store, you’ll see three different categories of table saws. There are entry-level models that are quite easy to get started with because they’re simple to use and require little investment. This is analogous to brute-force code generation. It’s very accessible because the investment in learning new techniques is small, and it’s a good fit for you if you only occasionally use the tool. These tools lack features, but with extra attention you can achieve high-quality output.
The store will also have some large, high-end, and extremely heavy floor models with large fixed out-feed tables. These models are cumbersome and expensive. This is analogous to the CodeDOM. The initial investment is quite large, and the ongoing maintenance is nontrivial. If you’re a full-time cabinetmaker, this might be a good tool, but for the average woodworker, it’s just too much. Similarly, I wouldn’t take on the long-term overhead of CodeDOM unless generating code in multiple languages was my project’s core requirement.
****************
5. To rip a board means to cut it lengthwise. There’s no doubt that some people can do it with only a manual tool. For example, hopeful miners built hundreds of plank boats from felled trees in a few months along the Yukon Trail. But it isn’t an easy, quick, or fun job!
The store will also have models that lie in the middle. These are the high-quality tabletop models. These have features such as powerful motors and adjustable blades, and they’re built for long-term durability. They’re not only the right tools for anyone with moderate needs, they’re the right tools for anyone needing capacity, portability to job sites, reliability, and ease of use but not wanting a high initial investment or long-term overhead. Similarly, I prefer XSLT for code generation because I think the medium level of initial investment pays off in the long term.
*******************
NOTE Both my shop and my brain tend to be a bit overcrowded. My table saw can also serve as a router table, saving significant space. Similarly, you can reuse skills required for XSLT code generation in working with the XML Document Object Model (DOM) of Visual Studio (which uses XPath). XSLT is also useful for other transformations such as preparing data for HTML display or modifying the format of an XML document.
Each of these table saws performs the same basic tasks. The differences between them would be quite significant as you were selecting a model, but a single book could explain how to build furniture with any of the saws. Similarly, this book covers how to build applications with any of the code generation mechanisms. Unless there are underlying differences between the three, examples in later sections of this book illustrate only XSLT to keep things focused on the core aspects of code generation beyond how you spit out code.
Table 1-1 and Table 1-2 summarize seven potential methods of code generation along with some benefits and drawbacks of each. I’ve included all seven available approaches to put them in perspective and show why only three are viable for your application development and discussed further in this book. Table 1-1 shows the mechanisms I’ll cover in this book, and Table 1-2 shows the mechanisms I won’t discuss further (also discussed in the earlier sidebar “Mechanisms Not Covered”).
| XSLT | BRUTE FORCE | CODEDOM |
|---|
| Covered in this book? | Yes | Yes | Yes |
|---|
| Viable for application | Yes | Yes | In some cases |
|---|
| code generation? | | | |
|---|
| Ease of searching | Easy | Difficult | Often impossible |
|---|
| Ease of pasting in | Easy | Difficult | Effectively |
|---|
| sample code | | | impossible |
|---|
| Multiple language | No | No | Yes |
|---|
| output from one source? | | | |
|---|
| Can generate stored | Yes | Yes | No |
|---|
| procedures? | | | |
|---|
| Restart generation | Continue | Restart | Restart |
|---|
| process for syntax fixes? | | | |
|---|
| VB option statements? | Yes | Yes | Yes, but |
|---|
| | | undocumented |
|---|
| Changeable indent width? | Difficult | Yes, with | Yes |
|---|
| | IndentedTextWriter | |
|---|
| Regions and vertical | Yes | Yes | To some extent, |
|---|
| whitespace? | | | but difficult |
|---|
| Other benefits | Relatively easy to | No new language | Code output in |
|---|
| read your code | or way of thinking | multiple |
|---|
| while developing | about code | languages |
|---|
| code generation | required | |
|---|
| Drawbacks | Requires learning | Hard to read and | Complex model |
|---|
| new language | maintain | with tons of |
|---|
| | | classes; very hard |
|---|
| | | to read |
|---|
Table 1-2. Four Mechanisms for .NET Code Generation Not Covered
| IDE |
|---|
| EXTENSIBILITY | | ENTERPRISE |
|---|
| REFLECTION.EMIT | CODEMODEL | VISIO | TEMPLATES |
|---|
| Covered in this book? | No | No | No | No |
|---|
| Viable for application | No | No | No | No |
|---|
| code generation? | | | | |
|---|
| Ease of searching | Difficult | Difficult | Impossible | Moderate |
|---|
| Ease of pasting in | Impossible (unless | Difficult | N/A | Moderate |
|---|
| sample code | you paste in MSIL) | | | |
|---|
| Multiple language | No, MSIL only | No | No | No |
|---|
| output from one source? | | | | |
|---|
| Can generate stored | No | No | No | No |
|---|
| procedures? | | | | |
|---|
| Restart generation | N/A | N/A | N/A | N/A |
|---|
| process for syntax fixes? | | | | |
|---|
| VB option statements? | N/A because | N/A | N/A | Yes |
|---|
| no language- | | | |
|---|
| specific features | | | |
|---|
| Changeable indent | N/A because no | Yes | No | Yes |
|---|
| width? | source code is | | | |
|---|
| produced | | | |
|---|
| Regions and vertical | N/A because no | Yes | No | Yes |
|---|
| whitespace? | source code is | | | |
|---|
| produced | | | |
|---|
| Other benefits | Can be used at | Integrated | Integrates | Unified project |
|---|
| runtime | into Visual | with UML | starting point; |
|---|
| | Studio IDE | model | allows Informa |
|---|
| | | | tion Technology |
|---|
| | | | (IT) control over |
|---|
| | | | environment |
|---|
| Drawbacks | Code written is | Can’t produce | Only creates | Only creates |
|---|
| MSIL, not a high- | VB .NET | initial stubs; | initial stubs |
|---|
| level language; no | output | very hard to | |
|---|
| high-level source | | use with VB | |
|---|
| code available | | .NET because | |
|---|
| for debugging | | of a Visio bug | |
|---|
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. |
Next: Breaking Down the Code Generation Process >>
More .NET Articles
More By Apress Publishing
| | |
| | |
| |  | |