Welcome to MSDN Blogs Sign in | Join | Help

Code Generators: when can you live with them?

Tom Hollander just posted a note Code Generators: Can't live with them, can't live without them. His question is really about the ROI on developing code generators. Most of the comments, however, are about the specific case he is trying to address with generators; creation of database mapping code.

Let me try to address Tom's key question: When it is worth creating a code generator as opposed to either (a) writing the code by hand or (b) trying to bake the logic (in this case db mapping) into a framework?

My experience with writing code generators is that their complexity grows very fast. Simple code generators, like the ones based on T4 templates used in GAX and DSL designers, are relatively inexpensive. However they suffer from the issues Tom outlines like, how to separate the generated code and the hand-written code, or how to generate and regenerated only the code that you need? 

As soon as you attempt to address those issue, the complexity of the generator sky rockets. Take the latest version of the Web Service Software Factory as an example. The code is selectively generated from models. You can generate code only for selected model elements, the entire model, or even connected models. However to do that, we had to create a code generation framework that extends the out-of-the-box T4 template-based code generation of the DSL toolkit. We also had to add a model validation framework to ensure that the models are consistent before we attempt to generate the code. Developing those mechanisms was a significant portion of the cost of building the factory. Our estimate, which we presented at the last p&p Summit, was that we spent over 50% of the effort on building the factory infrastructure, which did not include writing templates, code generation strategies, etc.

As another example take the LINQ-to-SQL modeling tool. It selectively generates LINQ code, but to do that it introduces a model that sits between the database and the code generators.

Bottom line, unless simple code generator addresses your needs, think very hard about the cost of building a more sophisticated generator. Almost always you end up introducing an explicit or implicit model of what you are generating or the generation mappings. However if you really need to do that, take a look at what we have done in the Web Service Software Factory and see if you can reuse the mechanisms we developed.

Wojtek

Published Sunday, November 18, 2007 2:04 PM by Wojtek Kozaczynski

Comments

# buzzlicias Blog Archive » Code Generators: when can you live with them?

# re: Code Generators: when can you live with them?

Friday, February 29, 2008 8:37 AM by rilcy

Hi,

Nice, informative, post.

Though I'm definitely late catching up on that one, I'd like to share with you my experience with building what I think is a pretty "agile" code generator in the context of a DSL -VS 2K5 SDK- (which mostly targets the technical, "plumbing", layers -much less the business ones) of three-tier WinForms/.NET remoting/SQL back office applications. The code generator commands are invoked from a "crafted-once-for-all" simple dialog box that you bring up within the designer via a contextual right-click on the instance model's items (or on the white model root background).

As the main user of my tool (e.g., as I'm also an application developer) I found that approach pretty handy. For this target architecture above, I generate what I think is about 95% of the code -- remaining handcrafted code mostly is about business rules and business process specifics that cannot not be modelled yet, because of natural language-written specs).

Of course, the generated code compiles without warnings nor errors, and runs on the three tiers, right after the code generation.

Feel free to let me know if you wish to know more about the code generator's code specifics themselves. But I plan to eventually make a paper about it anyway.

My two cents.

# re: Code Generators: when can you live with them?

Monday, March 10, 2008 4:35 AM by raca

I would like to make a software factory or some DSL tools for Dotnetnuke custom development.

I would be very interested in finding more about your code generator. I used Codesmith and added extensions that allowed me to do code regeneration via Preserve region code regions.

I would like to use latest VS 2008 software factories to achieve project items and code.

My email is radoslavATeverestkcDOTnet

Thanks

Rad

Anonymous comments are disabled
 
Page view tracker