A better way to auto-run T4MVC when you build

Update: also see the newer post on this topic


T4MVC has always struggled with finding the right way of running automatically when you build.  Being a T4 template in your project, by default it just runs when you save it.  This is not great, because you really want it to rerun whenever you make a change that affects it: e.g. new controller action, new static file, …

So I had come up with a pretty crazy workaround (the AlwaysKeepTemplateDirty flag), described in details in this previous post (under “The T4 file automatically runs whenever you build”).  It’s a pretty hacky technique, which mostly works but has some nasty quirks:

  • You need to open and save it once to start the save ‘cycle’
  • You need to leave it open
  • It’s always marked as dirty, which is an annoyance.  The little ‘dirty star’ is always there, and when you close the project you’re always prompted if you want to save it, even though you didn’t change it.

Recently, Danish developer Joachim Lykke Andersen asked a question about this on StackOverflow, and then proceeded to find a pretty nice solution to it himself, which he has now blogged.  His solution relies on handling the VS OnBuildBegin event.  So basically, you just do a one time step, and then you don’t need to worry about it.  I won’t repeat all the details here, so get them on his post.

He also discusses the possibility off turning this into an Addin, which would be yet cooler.  And maybe with some convincing he will, so add a comment to his post :)


POLL: should we kill the AlwaysKeepTemplateDirty flag?

Given that this is a cleaner solution to this issue, I’m thinking of removing the AlwaysKeepTemplateDirty logic.  At the very least, I could turn it off by default in the T4MVC.settings.t4 (currently it’s on by default).  But in the end, it’s a hack and I wouldn’t mind getting rid of it altogether.

Would anyone object to this, or have concerns about this new approach?   Just let me know in the comments…