Outputting Code - Working with the CodeDOM
(Page 19 of 19 )
I’ve just fluttered over the top of the CodeDOM, focusing on what it means to work with code as an abstract language-independent entity. Although this may seem natural to you if you took a compiler course, it’s a different way of looking at code than day-to-day creation in one or a small number of languages. Appendix D covers additional issues, incompatibilities, and limitations of the CodeDOM.
Most of the tips for efficient use of brute-force generation apply to the CodeDOM. You can’t output regions without snippets, so you may want to replace regions with comments; however, you’ll still want segments of your code output by individual methods. To maintain language neutrality in the CodeDOM, do the following:
- Avoid using snippets.
- Test your ability to compile the output in all target languages early and often.
- Test your component or application in all target languages.
- If you can’t follow the first tip, provide alternate code for all target lan guages and re-create the CodeDOM graph for each language.
If nothing else, you can hand this second part of this chapter to your boss if she thinks you should use the CodeDOM. Comparing these three methods of code generation illustrates just how expensive CodeDOM is for developing applications.
Summary One-click code generation allows anyone to precisely regenerate your code now and in the future. Reproducible one-click generation relies on a script. The script used in this chapter is a set of XML directives.
The keys to success for XSLT code generation are as follows:
- A single entry-level template accessing the root.
- A single high-level processing template called in the context you’re processing.
- A named template for each region in the output after you organize your sample code into regions.
- Adding nested named and match templates while regions become longer and more complex.
- Using match templates for any looping elements unless the output is nearly trivial.
- A separate supporting stylesheet to contain reusable utility templates. The keys to success for brute-force code generation are as follows:
- Break metadata extraction away from code outputting.
- Split outputting into individual classes that each output a single file pattern.
- Within each class, use an entry-level method.
- Use a different method for each region.
- Use additional methods while regions become longer and more complex.
- Use supporting methods for reuse wherever possible.
The CodeDOM abstracts your code sufficiently to generate code in any one of several languages. This abstraction makes working with the CodeDOM extremely complex. Seriously evaluate whether the extra time involved in working with the CodeDOM is justified for your application.
Additional Reading To find more information about the topics covered, try the following resources:
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. |
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |