Code Generation with T4

I was first exposed to T4 in January/February of 2008 when I was ramping up for a project that used the Guidance Automation packages. Even though that fell through, it was still worthwhile for getting exposure to a cool little utility like this.

T4 stands for Text Template Transformation Tool and is a built-in feature for Visual Studio 2008 and as an add-on for 2005. It’s an engine that can be used for generating code from any data source you want.

Add a template
In any project in VS, right click and Add -> New Item., select a text file and rename it so it has the extension .tt. This keys Visual Studio into the fact that it’s dealing with a T4 file.

Now would also be a good time to install the Clarius add-on for T4.

This tool gives you a bit more design time support than you normally get with T4 out of the box. Note to the faint of heart: Unless your willing to shell out for the not-free editions your about to step back in time to a place before Intellisense, AutoComplete, Syntax highlighting and other things we’re all accustomed to.

The best way to think about T4 is back to web scripting languages like PHP, ASP, etc. You have your template, and you have blocks of code for control flow for logic. Let’s take a look at a simple template

[code lang=”xml”]
<#@ template language="C#" debug="true" hostspecific="true" #>
<#@ output extension="txt" #>
for(int i=0; i < 10; i++)
I’m a template on line <#= i #>

Which tells us the control flow is done in C# and the output is a text file. When you save the file the template engine will run. You can also run the engine by right clicking and choosing “Run Custom Tool”.

You can see our output is now nested under our item in Solution Explorer.


There are two tags to be aware of here <# #> and <#= #>, the first allows for control flow code to be inserted and the second one is for outputting values.

That’s enough for a start, I’ll be back in another post about going a little more in depth.

Nice Kindle 2 Case

I don’t plan on product endorsements often on here, but thought I’d share this.  Magenic gave all of us Kindle 2’s for the holidays this year.  I’ve been hunting for a case since I got mine and wasn’t impressed with a lot of the stuff I was seeing on Amazon.  The quality wasn’t there or it was just too expensive.

I was in Staples today and spotted this:  It’s a Swiss zip folio that has a perfect area for a kindle and a couple other pockets.  Only $30 too.

Repeating Tablix Headers In SSRS 2008

So I’ve been trying to figure out haw to repeat headers in a Tablix.  I don’t do a lot of SSRS stuff and the report I’ve got is moderately complex, it does a bit of an involved grouping situation with a few sub reports for details and then a Tablix to repeat individuals under each details section.

The Tablix has Repeat Column/Row Headers properties in the property pane but they are useless.  During my searches I saw something about these properties being for when the report is too wide, not too long.

Anyways, at the bottom of your report designer there should be the grouping info pane.  Click on the black arrow in the upper-right corner of the pane to enable “Advance Mode”  Doing this shows static group items in your grouping pane for things like header rows.  Find the static item that corresponds to your header row and check the property pane.  There will be a “RepeatOnNewPage” property, set it to true and headers should repeat, at least they did for me.

Hint: If you can’t figure out which static grouping could be your row header, watch the report designer as you click on the different groups, it will highlight the one you just selected in the designer.

Blog Refresh

So this used to be my personal blog.  After a while that moved so my fiancee and I could blog together, this site has been languishing for quite a while.  I’ve kept meaning to repurpose it as a tech blog, and now I finally got around to it.

I’ve upgraded to WordPress 2.7 and am working on getting this mirrored at Magenic’s blog server.  The first thing I’ll probably be talking about is my trials in writing IM functionality into a kiosk using the UCC SDK.

Writing an IM client for Office Communications Server

So I’ve been working on adding in IM functionality for the kiosk software I wrote and maintain at Magenic. At first I planned on using the Office Communicator Automation API. It all worked in my proof of concept, except for one thing.

I couldn’t figure out how to determine when I was receiving messages. I could start conversations, detect when they’d been started with me, but not when I actually got a message.

Not too mention it felt kinda dirty because you’re really just automating the Office Communicator App, so you have windows popping up all over the place. In my case they would have been all behind the kiosk app so I wasn’t too concerned, but it still felt unclean.

My research led me to the Unified Communications Client API, which sounds like it’s what I wanted from the start.

Moral of the story, if you want to do more then simple presence and conversation initiation in your application, use the UCC API.

About the UCC API:

Why does Control.Parent exist?

Encapsulation is one of the major tenets of object orient programming. The ability to not worry about how things work behind the scenes and just use a component is lovely.

Recently, I’ve had to be a on a project where a lot of UserControls have been created and tons of them reference their parent property internally to get some bit of data that they need for this or that.

First of all, not only have you completely broken encapsulation by doing that, but now you can’t use that UserControl on any other form.

My favorite part of encapsulation is easy refactoring. We all make mistakes and wish we would have written something differently, and if we have the time to correct those mistakes we should. Recently, a developer wanted to give the UI a bit more user-friendliness so he threw a bunch of splitter panels on a form with some grids and UserControls to let the user customize the form a bit.

The problem is one of the UserControls referenced it’s Parent property. So now instead of ((parentForm)this.Parent).SomeProp I had to go back and fix the bug he introduced by doing this ((parentForm)this.Parent.Parent.Parent.Parent.Parent).SomeProp just get it to run. I realize shortcuts can save a ton of time in some cases, but why is Control.Parent allowed to exists when it makes it so easy to break encapsulation and lead to big problems with refactoring down the road.