Writing Code Generators with the Code Document Object Model - Part 1, Page 2
Reviewing the Macro Steps for Creating a Code Generator
Now you have an introductory understanding of the CodeDOM Architecture. To review, it is comprised of classes that inherit from CodeObject and each of these classes represents a code construct in .NET. When you are writing a code generator you are orchestrating these CodeObjects in much the same way as you would if you were actually writing the code. The CodeDOM creates what is referred to as a graph. By picking a specific provider the CodeDOM will perform substitution, substituting CodeObjects in the graph for literal code. In general terms, here are the macro steps for writing a code generator:
- Create an instance of the root code graph element, a CodeCompileUnit
- Add a namespace to the root graph element
- Add types to the namespace
- Add members to the types, including methods, properties, events, and fields
- Add lines of code to the methods
- Request a specific provider and a code generator from that provider
- Generate the code from the graph, writing the output to a file stream
- Compile the output source code
In theory writing a code generator is not too difficult. However, actually writing a code generator does require careful attention to detail and a lot of code.
There is no way Microsoft could know in advance what all of the types programmers would return from an XML Web Service. Caught between a rock and a hard place, Microsoft wanted to make Web Services easy to consume, but there is no one-size fits all client-side proxy. The solution was found in the CodeDOM. The CodeDOM uses Reflection to discover all of the public properties of types returned from a Web Service and with this knowledge spits out a dynamically generated client-side proxy. The proxy class hides the most difficult aspects of calling a Web Service, making Web Services much easier to consume. The CodeDOM can by used by you to support similar kinds of scenarios.
If you find yourself implementing the same pattern many times, or want to ensure that advanced patterns are coded correctly even by junior programmers then write a code generator to generate the code for them. Writing the code generator (see Part 2 of this article) takes more work then writing the actual code, but the generator can dynamically write different versions of the same pattern based on input parameters. For example, if you write a code generator for a typed collection then you could pass in the type to collect and the code generator will write a flawless typed collection every time, assuming the generator is written correctly.
Check out part 2 of this article—same Bat time same Bat channel—for a relatively complex code generator that will give you plenty of examples to work with.
About the Author
Paul Kimmel is a freelance writer for Developer.com and CodeGuru.com. Look for most recent book "Advanced C# Programming" from McGraw-Hill/Osborne on Amazon.com. You can also look for his upcoming book Visual Basic .NET Power Programming from Addison-Wesley. Paul Kimmel is available to help design and build your .NET solutions and can be contacted at email@example.com.
# # #