The Curly Brace Tax
Sunday, October 05 2008 11 Comments
(I shamelessly stole this title from a chat with Matt Hinze)
99% of the examples you see out there for ASP.NET MVC are using WebFormsViewEngine. That's fine, it's familiar, it benefits from intellisense, compilation, and refactoring support. But all of that comes at a price, and that price is (at times) incredibly wordy.
Picture this example, taken from the Site.master (master page) in the Preview 5 new project template.
At the top of the page, they want to render some text if the user is logged in, and different text if the user isn't logged in. Here are the screens:
This is implemented using ASPX code that looks like this:
<%if (Request.IsAuthenticated) {%>Welcome <%= Html.Encode(Page.User.Identity.Name) %>![ <%=Html.ActionLink("Logout", "Logout", "Account") %> ]<%}else {%> [ <%=Html.ActionLink("Login", "Login", "Account") %> ]<%}%>
Now let's take the same example and convert it to NVelocity, another view engine with a looser syntax:
#if ($isAuthenticated) Welcome $html.encode($user.name)! [ $html.actionlink("Logout", "logout", "account") ] #else [ $html.actionlink("Login", "login", "account") ] #end
We'd have to stuff the $isAuthenticated and $user values into ViewData, but that's a piece of cake. This is a great example of how concise we can get if we don't rely on all that strong typing. The beauty of this is, the key that you use for ViewData becomes the object you interact with on the view.
In NVelocity:
- case doesn't matter
- no need to open up <% %> tags, you can embed it directly in your template
- type doesn't matter. It's evaluated at runtime.
There are some downsides, however:
- You don't get compile time checking for your views. If I wrote $htlm.actionlink(..) I'd get an error at runtime.
- Performance. Compiled views are much faster than interpreted ones. It's likely that this doesn't matter for most sites out there.
- No intellisense. (The arguments to ActionLink above are not obvious, so you just have to memorize it)
- No Refactoring support. This is a big one. If you rename your actions, you'll have to do a string comparison search to get the various links you might having lying around in your view.
That last one is really the only one that I miss when doing something in NVelocity. In any case, try it out, see what you think!


Chris Missal
10.05.2008
7:29 PM
The example is fairly concise, and I'm working in pages that have a lot more going on than that. I like it, it's clean. Maybe we'll have to adopt NVelocity for a future release... :D