LLBLGenPro about the same time and recommended it to me.. ">
KnowDotNet NetRefactor

Book Review: Code Generation in Microsoft .NET - Kathleen Dollard

by William Ryan
Print this Article Discuss in Forums

Code Generation in Microsoft .NET is one of the most unique books of it type, mainly because of it's subject matter.  Let me start of by saying that this book probably isn't the best read for beginning .NET Developers because the subject matter is rather advanced.  But I'm ambivalent making that previous statement because I really wish I had come across it earlier.  My main interest in it primarily was fueled by my interest in Object Relational Mapping. About a year or so ago, I came across a product called Deklarit which was nothing short of amazing.  Here's this .NET Add in that not only builds classes for you, but builds database structure for you and will even NORMALIZE your data for you as well.  When I first came across this product I was like "Yeah, sure" but then I started playing with it and found it mind boggling.  While Deklarit isn't an Object Relational Mapper per se, it does end up doing what ORM tools do.  A close friend of mine started using LLBLGenPro about the same time and recommended it to me..  Again, I sat there and was amazed at how these things worked.  So when Kathleens book came out and I saw that there was a whole chapter dedicated to implementing ORM, I had to buy it.  At the time though I was spread pretty thin and didn't have time to really dig in to code generation.  In reality I didn't have the time not to investigate it.

Although I think just about everyone can benefit from code generation, I think it's one of those areas who's time has yet to come.  Not because it's not a tremendous feature, but because people still associate it with CASE Tools or 4GL's.  Well, let me start off by saying that it's like neither.  

Every time you create a class in Visual Studio .NET, code generation is happening.  In C# for instance, a default namespace is added for you as well as the class construtor. You can specify that your class will implement an interface and have it fill in the shell for you.  When you drag a DataAdapter on to a form, code generation occurs.  Sure, you can hand code your data access code or class template, but imagine how much time you'd waste doing this?  Imagine how many typos you'd make?  All that translates into reduced productivity and ultimately money.  So in a nutshell, this book is about doing the same types of things that Visual Studio .NET already does for you, it just expands upon them.  It's hard to imagine the case against automatic code generation because I don't think there is one.  But I just mentioned that I think it's time hasn't quite come yet.  Well, actually it has, people just don't realize it yet.  We use code generators every single day and probably quite a few times throughout the day.  And Kathleen's book does a phenomenal job on discussing the ins and outs of it and what can be done with it.

The first Chapter is probably the toughest to get through simply because your mind probably isn't used to thinking in these terms.  If you just breeze through it, you'll actually come away with the impression that it's a bunch of extra work for no benefit.  For instance, she uses a 'brute force' example, creating a text file with a streamreader.  Next she moves on to the CodeDom.  My first  impression was the same as that of Regular Expressions - really cool but too much hassle to learn at the moment (both of which I was initially wrong about).  Then she moves into an example with XSLT.  Jackpot!

Let me say this emphatically:  The discussion of XML, XPATH and XSLT could be ripped out of this book and put into another one that sold for the same price as this book and I'd still buy it.  I'm a little late on the XSLT bandwagon but have had to do increasingly more with it at work recently.  As I was running through a transformation that wouldn't do what I wanted, I remembered the rest of her book and 20 minutes later all was good in the world.  

The next two chapters move into extracting meta data and outputting code.  Again, this stuff seems like a lot of work if you are just casually reading.  I'd recommend not doing this because this is a critical piece of the code generation sales pitch.  Shortly thereafter she does a "Tying it all together" chapter that really makes you realize this is something worth learning and learning well.

The book shifts into part 2 at this point and starts on my favorite area, Object Relational Mapping.  Now, if you don't understand database structures and multi-tiered architecture - you probably won't like this section.  On the other hand, if you've done much with Data Access, you'll be a convert by the end of Chapter 6.  Coding Data Access Layer code is plain BORING.  It's straightforward but monotonous - probably the second most boring programming task next to writing reports.  Well, all of a sudden you realize that those days can come to an abrupt end thanks to ORM.  Sure, things like the Data Access Application Block can minimize how much DAL code you write but I assure you, this discussion will convince you that ORM is the real deal.  In particular, Chapter 8's discussion on Generating Middle Tier Templates is priceless because it's really easy to forget all about the middle tier for a lot of projects and revert to client-server stuff.  That's fine and dandy for some applications, but if you need something that can scale and morph, the middle tier(s) can't be ignored.

Another thing that makes this section stand out is that it's pretty intuitive.  When I originally got this book I pretty much skipped to Chapter 6, 7, 8 and didn't pay much attention to the rest.  That was because 1)  I was lazy and shortsighted 2) it immediately made sense when I was reading it.  I also think that if you are going to try to pitch code generation to management, this is the type of stuff that will increase the likelihood of them going for it.  

After this section, she runs you through a Winforms example application and an ASP.NET application.  At this point, you've learned what code generation can do, you'll have a good grip on how to use it and you'll see it in action.  At that point, it's time for you and your imagination to go have some fun and think of ways you can save yourself a LOT of time.  

Finally, the book concludes with the Appendices (which indicentally are just short of 200 pages).  Obviously you can probably tell by my earlier comments that I was pretty fond of this part of the book and I think you will too.  If you aren't using XPath and XSLT yet, START.  The power and flexibility it gives you is amazing and I can just about guarantee that you'll be able to think of at least 5 places that using them will save you a lot of time and hassle in the future after you run through two of her examples.  

In summary, this is one of those way outside of the box types of books because it's a way outside of the book area.  Most of us get paid to write code so the thought of having it done automatically is a bit discomforting.  It  shouldn't be.  What code generation offers is consistency (which will reduce the errors in your code) and freedom from doing the monontonous boring tasks that we all hate.  That will free you up to move onto cooler things like learning Biztalk or Commerce Server.  The uniformity that you can gain from using template based code will also help you implement standards which is never a small task. (Yes, I know about Enterprise templates, but if anything I think they are complimentary as opposed to being different).  There's a time investment associated with all of this and this is NOT  a book that you can just breeze through and get a lot out of.  The author obviously put a lot of time and effort into writing it and to get the most out of this book, you'll need to as well.  There's just as much emphasis on learning how to think and implement code generation as there is on how to actually do it.  It's not as easy as copying code from a Cookbook but I can safely say that if you are willing to make a minor commitment to working through everything here, the return on investment will be surprisingly high.

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing