One of the immense advantages that ASP.NET MVC has over classic ASP.NET is the clean separation of concerns between the presentation and controller logic. Unfortunately, while the MVC RC has brought with it many improvements, it has also taken a step towards breaking down the clean separation of concerns with the introduction of the JavaScriptResult ActionResult. Per the release notes, "the JavaScriptResult class is used to execute JavaScript code that is created on the server and sent to the client." The release notes even go as far to include the following code example:
While I can surmise that there is more than one useful scenario for the inclusion of JavaScriptResult, the above example should be seen as an anti-pattern of its use. This example is introducing a very fragile dependency into the controller on the view itself. In classic ASP.NET, the code-behind page was able to have strongly typed access to elements in the view. In the above scenario, a similar dependency is introduced into the controller, but now it's not even strongly typed.
Like extension methods and lambda expressions, JavaScriptResult puts more power into the hands of the developer, but also assists in shooting yourself in the foot if used improperly. So like any powerful .NET tool, have a good reason to use it, keep an eye on proper separation of concerns, and always keep testability and maintainability strongly in your design.
Billy McCafferty
Posted
02-07-2009 8:38 PM
by
Billy McCafferty