Let’s Talk Templates

Code generation templates have several competing pressures – they need to be easy to use and they need to be powerful. They need to be flexible but encourage best practices. If you ever say “my templates got me started then I had to customize them” you’re templating can be better. I’m not aware of any situation where you can’t isolate the handcrafted code from the generated code sufficiently to keep code generation active for the entire life of your application.

When I say full life cycle, I mean from initial prototypes to finally pulling the switch in a decade. Yep a decade. Code generation should aim for evolution and extremely long project life cycles. The metadata of the project can far outlive the technology, even outlive the database. That requires a deep commitment to code generation, and if you don’t have that commitment, the flip side is that code generation can pay back today. At that moment when it feels easier to pull a file out of code generation, or to repeat a similar block of code in two places instead of pushing it into your code gen templates, that’s where the commitment comes in. With or without it, you get payback; it’s just greater the deeper your commitment is to the metadata and templates.

My current client pushes me because he is even more committed to the decade long lifecycle based on code gen than I am. And he’s right. Every time he’s pushed to get something into templates or do code gen better, it’s taken less work and gotten us further in the short term than I expected.

What if you could combine the simplicity of CodeSmith with the power of XSLT?

And what if you could do it primarily in a language and architecture you already know, or should be learning anyway?


This is the first post in a blog series to present this templating style. I also covered in a.NET Rocks show I recorded yesterday. I’m very excited about it, but I want to introduce the basics before I tell you the real punch line. This template style is the best way to generate code today and it’s based entirely on tools you’ve got on your desktop right now.

Look for more here and in the .NET Rocks TV episode scheduled to appear Feb. 15th. The beautiful thing about the techniques I’ll show is that you can smoothly transition from simple to complex templates dialing in the exact complexity appropriate for your organization and application. And, just wait for the punch line.

5 thoughts on “Let’s Talk Templates”

  1. I have to admit, you are really going to have to sell me on code generation. I used code gen techniques a couple of years back, and I know things have considerably changed since then, but I have also discovered other techniques that enable me to write less code and don’t require generation either. So, I’m counting on a really good punch line…

  2. Vish,

    Teh TTT stuff is interesting. I have not put much time into it, partly because I haven’t gotten into the domain specific model. It does allow you to access input data outside the domain model, meaning you could access metadata similar to other generation approaches. But if you’re doing that, I’m not sure how much the MS DSL model is giving you. I’ll be intersetd if you have a different perspective as you explore it – particulary what the tool is giving you.


  3. Kenny,

    What problems did you encounter?

    Many are related to architcture rather than code generation, or arise from different environments (pre-OO, no generics, etc) that make code generation more challenging.


Leave a Reply

Your email address will not be published. Required fields are marked *