Geeks With Blogs
Brian Schroer Don't Call Me Mort!
Technorati Tags: ,,

 

I’m working on my first ASP.NET MVC project, and I really, really like MVC.

I hate all of the “magic strings”, though:

    <div id="logindisplay">
        <% Html.RenderPartial("LogOnUserControl"); %>
    </div>
    <div id="menucontainer">
        <ul id="menu">
            <li><%=Html.ActionLink("Find Dinner", "Index", "Dinners")%></li>
            <li><%=Html.ActionLink("Host Dinner", "Create", "Dinners")%></li>
            <li><%=Html.ActionLink("About", "About", "Home")%></li>
        </ul>
    </div>

They’re prone to misspelling (causing errors that won’t be caught until runtime), there’s duplication, there’s no Intellisense, and they’re not friendly to refactoring tools.

 

I had started down the path of creating static classes with constants for the strings, e.g.:

            <li><%=Html.ActionLink("Find Dinner", DinnerControllerActions.Index, Controllers.Dinner)%></li>

…but that was pretty tedious.

 

Then I discovered T4MVC (http://mvccontrib.codeplex.com/wikipage?title=T4MVC).

Just add its T4MVC.tt and T4MVC.settings.t4 files to the root of your MVC application, and it magically (and this time, it’s good magic) generates code that allows you to replace the first code sample above with this:

            <div id="logindisplay">
                <% Html.RenderPartial(MVC.Shared.Views.LogOnUserControl); %>
            </div>
            <div id="menucontainer">
                <ul id="menu">
                    <li><%=Html.ActionLink("Find Dinner", MVC.Dinners.Index())%></li>
                    <li><%=Html.ActionLink("Host Dinner", MVC.Dinners.Create())%></li>
                    <li><%=Html.ActionLink("About", MVC.Home.About())%></li>
                </ul>
            </div>

It gives you a strongly-typed alternative to magic strings for all of these scenarios:

  • Html.Action
  • Html.ActionLink
  • Html.RenderAction
  • Html.RenderPartial
  • Html.BeginForm
  • Url.Action
  • Ajax.ActionLink
  • view names inside controllers

But wait, there’s more!

It even gives you static helpers for image and script links, e.g.:

    <img src="<%= Links.Content.nerd_jpg %>" />
 
    <script src="<%= Links.Scripts.Map_js %>" type="text/javascript"></script>

…instead of:

    <img src="/Content/nerd.jpg" />
 
    <script src="/Scripts/Map.js" type="text/javascript"></script>

 

Thanks to David Ebbo for creating this great tool.

You can watch an eight and a half minute video about T4MVC on Channel 9 via this link: http://channel9.msdn.com/posts/jongalloway/Jon-Takes-Five-with-David-Ebbo-on-T4MVC/.

You can download T4MVC from its CodePlex page: http://mvccontrib.codeplex.com/wikipage?title=T4MVC.

Posted on Sunday, June 6, 2010 7:47 AM | Back to top


Comments on this post: Hello, T4MVC – Goodbye, ASP.NET MVC “magic strings”

# re: Hello, T4MVC – Goodbye, ASP.NET MVC “magic strings”
Requesting Gravatar...
You should also try and modify the default t4 templates for the mvc project.
Left by hector minaya on Jun 06, 2010 9:45 PM

# re: Hello, T4MVC – Goodbye, ASP.NET MVC “magic strings”
Requesting Gravatar...
Hi Brian,
I think your not considering that the goal of MVC is decoupling models, controllers and views and I think you are breaking this concept by referencing directly a controller action in a view. That is why the asp.net framework uses strings.

Kind Regards,
Jorge
Left by Jorgebg on Jun 22, 2010 3:53 AM

# re: Hello, T4MVC – Goodbye, ASP.NET MVC “magic strings”
Requesting Gravatar...
It's still using the same strings and generating the same HTML, but with Intellisense. I don't see how it's more tightly coupled.
Left by Brian Schroer on Jul 09, 2010 7:30 AM

# re: Hello, T4MVC – Goodbye, ASP.NET MVC “magic strings”
Requesting Gravatar...
@Jorge At its core, MVC relies on convention. The problem with this is that you can't compile in convention. Therefore you run the risk of having an application which compiles (and most likely passes tests as well), but does not run. This approach uses that convention to build some static methods which you can use instead of string constants. As soon as you type something like Html.ActionLink("Text", "ActionName", "ControllerName") you have referenced a controller in your action view. Your view and controller are now tightly coupled (again based on convention). Generating that string in code just allows you to ensure that your controllers are properly identified at compile time. This approach is a pretty widely accepted MVC best practice even if done without T4.
Left by Doug Lampe on Aug 02, 2011 11:30 AM

Your comment:
 (will show your gravatar)


Copyright © Brian Schroer | Powered by: GeeksWithBlogs.net