views:

50

answers:

2

I've noticed a pattern that can make refactoring MVC2 apps difficult. When you change the name of an argument for an action you must update the values everywhere that action is used. For example,

public ActionResult List(string p)

in the view

<%= Html.ActionLink("List", "Directory", new { p = "somePath" }, null) %>

What if I want to change p to be more descriptive such as path.

public ActionResult List(string path)

This will mean that everywhere in the view where I've specified p = "somePath" it must be changed to path = "somePath". I see this as being tedious to track and maintain.

A static analysis of this seems to be the right solution for keeping arguments and parameters consistent. I know resharper has an indication if an action or view doesn't exist. I imagine it could just as easily detect parameter naming conflicts.

+1  A: 

You could use the Lambda version of ActionLink

 <% Html.ActionLink<HomeController>(a => a.Index(), "Home");  %>

which will give you compile time checking and strong typing.

StressChicken
As far as I'm aware this is still confined to MVC Futures. It's not included in the release version of MVC2.
LukeH
This only solves the problem of Actions and Controller names, not parameters AFAIK
Hadi Hariri
This seems like the beat solution I've seen. Unfortunately, mvc futures doesn't support complex types as parameters to an action. It works really well for primitives though.
Joe
+1  A: 

You can use the T4MVC to generate strongly typed references to things that are normally represented as strings in regular MVC, for example:

<% Html.ActionLink("List", MVC.Home.Directory("somePath")) %>

You can read more about T4MVC and download it from here: http://mvccontrib.codeplex.com/wikipage?title=T4MVC

marcind
This is nice but I don't like how it modifies my controllers. It makes them partial and makes all of my actions virtual. I understand why. But I still don't like it.
Joe