Here’s something I wanted to do for a long time. The Microsoft AJAX library brings order to the chaos of JavaScript development by introducing familiar concepts: namespaces, classes, interfaces, enumerations, property setters and getters, etc. The downside is the laborious and error-prone manual coding of all that goodness.

My first impulse was to write JavaScript snippets for Visual Studio 2008. After two hours of fiddling, it seemed Visual Studio wouldn’t budge. Then I saw Jeff King’s comment on ScottGu’s blog that they won’t be supported in VS 2008. Bummer. They would’ve been awfully handy.

CodeSmith with Microsoft AJAX templates

Given the popularity of CodeSmith and its templating markup language I decided to give it a try. Eventually I put together a set of templates which code-gen the following:

To produce these templates, I’ve gone through the official docs from A to Z, as well as bevy of samples found on blogs. I was looking for common patterns of implementation, so the templates represent my best shot.

For example, in an event handler of a behavior control you will probably manipulate the associated element. In a client control you will probably allow the caller to add their own event handlers, so you won’t need to manipulate the associated element yourself. These are general cases, and you certainly don’t have to follow them by the book, but that’s the kind of boilerplate code the templates generate.

Links

Download templates and watch the video (safe for work; no dancing strippers, I promise) from the CodeSmith Gallery.

Acknowledgements

I’d like to thank Eric Smith’s crew for a free license of CodeSmith. Learning its markup language is a breeze and there are plenty of templates “out of the box” and up on the web site.

Also, big thanks to TechSmith for a free license of Camtasia which allowed me to put together the screencast. It’s amazing what this product can do!