This post is over 6 months old. Some details, especially technical, may have changed.

T4MVC: Strong Typing vs Magic Strings

Magic strings are everywhere in ASP.NET MVC 2.  Less so than in version 1 and some of the Beta and RC releases but there are still some kicking around.  The problem with literals is that they don't give you any compile time error checking or refactoring ability.  Say for example you have an action link that points you to the Edit action of the Person controller - you write it like this.

<%=Html.ActionLink("Edit", "Person", new { id = 12 }) %>

Now image you've written something like this over numerous pages and all of sudden you change the name of the Action or the Controller.  These are literal strings so you can't use the refactor tools in VS or get any compile time error when you build.  The only real way to ensure everything has been changed would be to do a quick regression test.  Nightmare.


T4MVC makes use of the Visual Studio bundled T4 Text Template Transform Toolkit (Template based code generation tool) to generate invisible classes that give you strongly typed links to Controllers, Actions, Views and even JavaScript, CSS, Images and other static links.

T4MVC is part of the MVCContrib project (though completely standalone).  You simply download the zip with the 2 T4 files and add them to the root of your MVC project and you are done.  Here are some examples of it in action.


<% Html.RenderPartial("DinnerForm"); %>
<% Html.RenderPartial(MVC.Dinners.Views.DinnerForm); %>

And within a controller

return View("InvalidOwner");
return View(Views.InvalidOwner);


<%= Html.ActionLink("Delete Dinner", "Delete", "Dinners", new { id = Model.DinnerID }, null)%>
<%= Html.ActionLink("Delete Dinner", MVC.Dinners.Delete(Model.DinnerID))%>


You also get the ability to reference static files so that if they are moved or renamed you'll not be missing images etc. - something that is a pain to test most times.

<img src="/Content/nerd.jpg" />
<img src="<%= Links.Content.nerd_jpg %>" />

Another nicety is that you also get this for JavaScript files but without having to do anything it will swap out debug (uncompressed) versions of you files for minified versions when you move into production.  As long as you have <filename>.js and <filename>-min.js side by side it will determine which one to use based on the build environment.

<script src="/Scripts/Map.js" type="text/javascript"></script>
<script src="<%= Links.Scripts.Map_js %>" type="text/javascript"></script>

Summing Up

T4MVC is a nice tool to get around magic strings.  There are other things out there that give "fluent" syntax to methods that use literals which use delegates instead of generating any sort of proxy classes.  Which is better is a question I still can't answer confidently.  I have had some minor issues using T4MVC (mostly my fault) and I am a bit dubious about the syntax for static files but it's a decent starter for 10 and doesn't need any thought to implement and use.  Of course you could also roll own specific T4 template (or extend T4MVC) but that obviously requires a bit more work.

Published in .NET on October 17, 2010