Outputting Code
(Page 1 of 19 )
For software developers who desire the easiest way to get a job done well, one-click code generation is like water travelling around rock. This chapter provides a preliminary look at a tool, the harness, to manage the multistep process of code generation. (From the book
Code Generation in Microsoft .NET by Kathleen Dollard, Apress, 2004, ISBN: 1590591372.)
Outputting Code Principle #3: You, or someone unfamiliar with project, can regenerate your code precisely as a one-click process—now or at any point in the future.
Once you've prepared your metadata, the next step is to build templates and perform code generation. Your initial development and long-term maintenance both benefit from repeatable one-click code generation. You benefit from operational simplicity because you can largely forget about code generation details and template content during development. A maintenance programmer benefits from operational consistency because they can repeat your actions with little effort. The keys to this repeatability are placing as much code as practical in templates and using a tool to perform the multiple steps of code generation.
Managing the complexity of sophisticated code generation is one of the key solutions this book provides. You can manage this complexity by combining the following three approaches:
- Splitting code generation into five distinct steps with five corresponding principles (covered in Chapter 1)
- Using a tool to manage the multistep process of code generation (covered in this chapter)
- Building templates that reflect best practices (covered in Chapters 6–10)
NOTE: Building templates to hold the structure of your code is trivial for trivial cases. When it looks complex, keep in mind that I’m unwilling to relegate code generation to simple or trivial cases. I believe your code generation techniques should be able to handle any coding problem you choose to address and produce the code you want. My job is to tell you the possibilities; you get to tune in the level of complexity that makes sense for your project by using as much or as little as you want. |
This chapter provides a preliminary look at a tool, the harness, to manage the multistep process of code generation, and Chapter 5 and Appendix C go on to cover this tool from different perspectives. Specifically, Chapter 5 contains more detail on using the harness, and Appendix C discusses its code.
Once you’ve seen how you’ll perform code generation via the tool, you can focus on how you create individual templates. The bulk of this chapter explores template creation, covering aspects of brute force, XSL Transformations (XSLT) templates, and the CodeDOM in additional detail.
Understanding the Elements of the Code Generation Harness One of the great parts of software development is that it thrives on a basic human trait—laziness. This isn’t irresponsibility, carelessness, or a lack of personal hygiene. It’s looking for the easiest way to get a job done well. You can work hard to cut corners in a responsible manner, and your source code often thrives as a result. Code reuse is a classic example, but object-oriented programming, code generation, refactoring, and other techniques benefit from the same trait.
You’ll perform a large number of steps in code generation. You’re likely to have the following:
- Several extraction, morphing, and merging steps to prepare your meta-data, as discussed in Chapters 2 and 6
- Five templates to create stored procedure scripts (Create, Retrieve, Update, Delete, and SetSelect)
- An installation step for each stored procedure script you generate
- At least four middle and data tier templates (generated objects, generated collections, editable objects, and editable collections)
- Up to ten separate User Interface (UI) templates
Rather than individually running these steps, you can build on your responsible laziness and create a one-click approach to code generation that easily accomplishes all of these steps. The frightening alternative is code generation that follows a two-page checklist. To transition from that two-page checklist to one-click processing, you need a code generation harness that processes a script consisting of individual instructions called directives.
Avoiding Too Many Steps Running a script lets you weave dozens of code generation steps together and run them with a single mouse-click. This lets you precisely repeat the generation process with a single click. Code generation must be repeatable. You’ll want to easily repeat it next week, and someone marginally familiar with the language and application may need to repeat it in three or four years to incorporate a database or business rule change. If they can’t do that, they’ll cut corners and hack around your solid design. Now, the real nightmare: A programmer changes a generated file directly when trying to get a job finished. Eight months later, someone else regenerates and introduces potentially catastrophic bugs in a part of the application no one expected to change—so it wasn’t even in the test plan! Code generation doesn’t have to be like that.
If you’ve played with the ADO.NET strongly typed DataSet, you’ve experienced the opposite of one-click generation—the stepwise process of generating code that can turn into a two-page checklist. First, you drag the tables or stored procedures onto the design surface. This generates an XML Schema Definition (XSD). You manually add relations and might adjust name mappings or add annotations. These changes modify the XSD. Then you select Generate Dataset from the context menu, and code generation creates the strongly typed DataSet. You have to do this for every DataSet—most of the steps each time you regenerate!
Forget that! Your goal is one-click generation. You put a lot into building your application. The scripted harness preserves your important choreographed steps through rounds of even not-so-bright maintenance programmers. Listen to your laziness gene and ensure that you’re using a one-click approach. That’s the only way to build stable code generation. Then ensure that the harness and everything it accesses is a permanent part of your project. Let your script and templates change for new projects, but split off them off as a semistatic part of each project. Change them only if needed because of a conscious decision and as part of the project’s evolutionary cycles, backed by testing. It’s not just convenience; it’s essential. Planning for the long term includes intelligent one-click code generation based on a script.
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: Understanding Script Directives >>
More .NET Articles
More By Apress Publishing