Freigeben über


An even better way to run T4MVC: a VS AddIn

Last week, I blogged about a nice way of auto-running T4MVC by using the Macro IDE to write an OnBuildBegin event handler.  This was a big improvement over the hacky ‘AlwaysKeepTemplateDirty’ flag that we’ve been using since T4MVC’s early days.

Since then, Wayne Brantley has taken this idea to the next level by turning it into a Visual Studio AddIn.  Check out his post and give it a try!

The nice benefits of the AddIn over directly handling VS events in the macro IDE are:

  • Simpler install: you drop a couple files in the AddIn folder and it just works
  • Encapsulation: all the code related to this is in one binary
  • Easy to disable: once the AddIn is installed, you can just go in Tools / AddIn Manager to turn it on or off

Note that Wayne’s AddIn works a bit differently from Joachim’s macro (and from AlwaysKeepTemplateDirty): instead of running when you build the project, it runs as soon as you save a file.  The advantage is that you can just Add a controller action and save the .cs file, and it will instantly be available to your views via T4MVC.

The downside is that it makes saving a little slower, but in practice it seems to be mostly negligible.  But it would be great to hear people’s feedback on this.  If there is a perf issue, we have some ideas on making it faster.

If you use T4MVC, please give this AddIn a try and let us know how it works for you!

 

Where are we on getting rid of the AlwaysKeepTemplateDirty flag?

In the last post, I also polled people on whether we should just kill this flag.  Mostly, people were ok with that, but some cautioned that we should keep it until the replacement has been show to really work well.  This sounds like a wise decision, and this remains true with this AddIn.  Hopefully the AddIn will work for everyone, and once we’re comfortable enough with that, we can think about killing the old flag.

Comments

  • Anonymous
    March 18, 2010
    Works a treat, was particularly pleased about the simple install, I'd expected an msi.

  • Anonymous
    March 18, 2010
    I've just tried it out on our solution, and it kills it. We have 35 controllers and hundreds of views and partials, and every time I save a file, I have to wait for about 30 seconds. Otherwise, it's a really good idea!

  • Anonymous
    March 19, 2010
    @Dan: hmmm, that's pretty bad. Strangely, I'm not able to repro that.  I added 250 actions to a controller (in the test NerdDinner app), and also 1000 bogus views under Views/Dinner, and it still takes under a second (on my 3 year old nothing-special home machine).  I wonder what's different about your project that makes it so slow.

  • Anonymous
    March 19, 2010
    The generated T4MVC file is about 7710 lines. I also have the 'SplitIntoMultipleFiles' bool set to false. We also have several projects in this solution (28 + 3 deployment projects). Most of those are unit test projects and the rest are layers to services and the db and other output projects. Assuming that the VS add-in doesn't make any distinction between saving a class in my mapper layer and one of my controllers in my MVC project, I assume that it'll get run regardless, and also the overall size of the solution may also contribute to this?

  • Anonymous
    March 24, 2010
    I vote to keep the old option around. I would rather not having to depend on an extra addin and I found that clicking on the transformation button is very effective.

  • Anonymous
    April 21, 2010
    The comment has been removed

  • Anonymous
    December 22, 2010
    Hi, David! I really like the work that's been done on T4 MVC, but I think I have a better solution to the Regenerate-on-Build problem. I just blogged about it: otac0n.com/.../Pain-Free-T4MVC.aspx This is actually documented (sparsely) in the MSDN section on T4 Templates: msdn.microsoft.com/.../bb126445.aspx

  • Anonymous
    December 22, 2010
    @John: interesting, I didn't know about this technique, thanks for blogging it!  I twittered a link to your post.