Share via


T4MVC now with multiple file support

Ok, so I’m indulging in self-publicity a little with this post, but I really want to take the opportunity to highlight some great work that David Ebbo has done. I’ve been spending a bit of time with ASP.NET MVC Framework and really like it. The combination of testability, being led towards separation of concerns, massive amounts of extensibility and the way it aligns to HTTP all resonate quite strongly for me. I’m not saying for a minute that Web Forms is dead, but I do believe it is getting some serious competition from the MVC Framework!

However, there has always been one area of the MVC Framework that just hasn’t sat well with me: magic strings. I.e. things for which the compiler doesn’t provide verification, refactoring won’t necessarily recognise, and intellisense provides no support! I’ve given a couple of examples below:

 return View("ResetSuccess"); // render a non-default for this action
 <%= Html.ActionLink("Details", "ShowCustomer",
 new { customerID="ABCDE" } )%>

In the examples above, “ResetSuccess” and “ShowCustomer” are used to refer to a view and an action respectively and are clearly magic strings. The “customerID” property on the anonymous type we’re creating is also essentially a magic string: the compiler can’t check that it actually matches a parameter on the action we are invoking. Fortunately there are ways around this. One way is to use the strongly typed helpers from the MvcFutures project. These let you use syntax along the lines of

 <%= Html.ActionLink<CustomerController>(c => c.ShowCustomer("ABCDE") )%>

This does now give you compiler checking etc, but I’m not entirely sure about the syntax and there have been some questions raised around the performance (although I have to admit that I haven’t had chance to verify that).

Another option (and the one that I’ve been using recently) is David’s T4MVC. You can download it from here and read more about it on David’s blog here, but to revisit the previous examples using T4MVC:

 return View(MVC.Home.ResetSuccess); // render a non-default for this action
 <%= Html.ActionLink("Details", 
 MVC.Customer.ShowCustomer("ABCDE") )%>

I find this a really nice way to navigate through my controllers and views. It’s probably worth pointing out that it also works for referencing partial views, setting up routes, … essentially any place I’ve needed to refer to items in code it has helped me so far. I won’t spend a huge amount of time going through how this all works but I would encourage you to take a look at the code that gets generated. David has done some really neat stuff to ensure that it all works with intellisense/refactoring by deriving a new class from existing controllers and using a custom ActionResult to capture the action parameters etc. The latest update has some minor changes by yours truly (although most of the work was already done thanks to Damien Guard’s Multiple File Manager!) to allow you to generate separate files for each controller to make it easier to work with T4MVC in a team with multiple developers checking code out.

Personally, I’m a huge fan of T4MVC and find that it provides a simple, discoverable way to reference views, actions etc. So, what are you waiting for? Go and download it now!

Originally posted by Stuart Leeks on November 17th 2009 here.