views:

42

answers:

1

I'm a big fan of compilation errors. I find the a lot easier to fix than runtime bugs. I currently using the ASP.NET MVC framework and I find it has a lot of room for typos that the compiler won't catch.

For Example if I want to return the view Data from the Index action.

public ActionResult Index()
{
   return View("Data");
}

can easily be mistyped

public ActionResult Index()
{
   return View("Dats");
}

and will not give an error till I run that action.

Has anyone devised a method of checking these sorts of things at compile time? I've come up with two possible solutions:

  • Creating a static class with all action names as constants, and using that for view names. I would have a coding rule that says only use the constants. The weakness with this if I change a view name I have to update the class.
  • Lots of unit tests just to make sure every controller action returns a valid ActionResult and running them anytime I make a change.

What process have y'all implemented to help manage this risk? Are there any add-ons or frameworks that mitigate this issue?

+4  A: 

The T4MVC project (which recently became part of MvcContrib) works to solve this issue. From the documentation:

T4MVC is a T4 template for ASP.NET MVC apps that creates strongly typed helpers that eliminate the use of literal strings when referring the controllers, actions and views. It helps make your MVC code much more maintainable, and gives you intellisense where you normally would not have any.

AJ
Does it work with the express versions of Visual Studio?
C. Ross