Code Generation in Microsoft .Net 
Buy it at Amazon
Author Kathleen Dollard
Publisher APress
Length 456 pages
vbRad Rating: 5 fingers up. (5 out of 5)
Reviewed by Robert Gelb

There were a lot of things that I found intriguing about this book. First and foremost is the subject. Just like Kathleen (as mentioned in chapter 1 or 2), I am quite lazy. I even made it a philosophy. So code generation aligns perfectly with my lifestyle.

I have written many code generators in my programming career. One very wise programmer told my many moons ago that "if you have to do something twice, then automate it". The last major one I've written generates a ready to use web application based on the heavily tweaked "Asp-Less" design. Here is an example of that one-click effort. The example is the minimal setup. The generator actually supports many levels of validations/options that can be setup right in the UI. Right out of the box you get listing of data with sorting/paging/editing/adding/searching/validating of data. In addition, the code is easily extensible. This is a passive generator though, however, it is good enough for 90% of my projects.

Another thing that attracted me to the book is that I know the author from DevX's once-hopping with discussion and now demised off.ramp newsgroup. We've had conversations (actually mostly bitter disagreements) regarding just about every known topic on earth. Regardless, I appreciate author's technical know-how and intellectual honesty.

The book describes several different techniques for generating code (XSLT, Brute-Force and CodeDOM) but really focuses on XSLT. As far as the actual n-tier design goes, she uses that of CSLA for .NET by Rockford Lhotka, not to be confused with Latka from the Taxi TV series. Though they may have the same accent, who knows.

I really liked the book, because it puts into words what I've long practiced and pretty much kept in my head. When comparing the pros and cons of various code generation methods, she mentions that when using XSLT, one actually has to know XSLT. This is actually a pretty big "con" and will probably be a killer for many programmers. Imo, XSLT is one of the most convoluted technologies to come out of W3C and learning it is just a steep learning curve. Why they couldn't simply provide a extention to the DHTML to do what XSLT does, I have no idea. However, once you get up to speed, it quite easy to use.

But anyway, the book goes through the basics of code generation like gathering metadata (that's a big word for database schema), building templates, and combining metadata with templates to produce finished result. Actually once you've read the first couple of chapters, you'll see exactly why XSLT is such a useful technology here: the XSLT processor does most of the grunt work of churning out code and writing it to appropriate files.

The next set of chapters goes through the task of actually generating a useful project using the CSLA design pattern. This is where you really learn about how to turn a fairly complex database schema into a project that you can use immediately. Kathleen does a bang up job here.

Finally, the book brings it all together by showing the reader how to generate the whole end-to-end n-tier app.

So XSLT is good because the engine will do a lot of work for you, but I am leaning towards a different approach, which, though similar to XSLT, is not described in the book. This approach is implemented by Eric J. Smith in his CodeSmith code generator. It is similar to XSLT because it also uses code in the templates to drive the generation. Except in his case, the code could be either C# or VB.NET. And I know those two a hell of a lot better than XSLT. The only downside to CodeSmith is that while the tool is free (actually, only the basic version is free), the source code is not. But both approaches are valid.

Bottom line: if you have never used/written code generators, time to get into it - it will make you a fantastic coder. This book will help to take you to that level. Remember, most of the time it's not about what a brilliant coder you are; it is about how fast you can churn out reliable apps, especially in these days of massive outsourcing. Secondly, the book will unintentionally teach you a lot about XSLT/XML. Lastly, you'll learn a bit on how to put together enterprize grade applications in a hurry.





Want more reviews and sample chapters? Click here.