BT

ASP.NET MVC 3 Embraces Dynamic Typing

by Jonathan Allen on Dec 13, 2010 |

Nearly a decade ago Microsoft gambled big on WebForms and static typing. With the dial cranked all the way over to full encapsulation, each page could almost be treated as its own program. In the intervening years the industry has largely gone in the other direction, favoring separation of concerns over encapsulation and late binding over early binding. This leads us to a two rather interesting questions.

The first is a question of terminology. When you consider the original Smalltalk MVC pattern, the views and controllers are not only tightly coupled but literally come in pairs. This is exactly what you would find in most Microsoft frameworks including classic VB, WinForms, WebForms, WPF, and Silverlight, all of which use a code-behind file for the controller logic. But these days when someone says “MVC” they usually mean a framework wherein the view and controller are very loosely coupled. This is especially true of web frameworks where HTML’s form submission mechanism allows any view to target any controller. Since this article is talking about web technologies, we’ll stick to the modern definition.

The second question is, “If you are Microsoft, how do you switch tracks without up without alienating your developers?” So far their answer has been, “By releasing a new version every year until you get it right.” The first production release of ASP.NET MVC was in March of 2009. March of 2010 saw the release of ASP.NET MVC 2.0. And with the 3.0 in its second release candidate, we should see a production drop by March of next year.

ASP.NET MVC 3.0 Release Candidate 2 was announced on December 10th. RC 2 builds on Microsoft’s commitment to jQuery by including jQuery 1.4.4, jQuery Validation 1.7, and jQuery UI in the default project templates. While they have been toying with moving away from server-side controls for a while, the inclusion of jQuery UI suggests that they are really taking it seriously now.

For those concerned with scalability, there is now much finer control over session state. Using the SessionState attribute, you can indicate to controllers whether session state should be read-only, read-write, or skipped entirely. This won’t matter for single-server websites, but can have offer huge gains when the session state has to be fetched from another server.

MVC 3 will include the Razor view engine. This engine HTML encodes output by default, reducing the likelihood that raw text will find its way on the screen. And even when there isn’t a risk of HTML injection attacks, a bit of unencoded text can easily prevent a page from correctly rendering.

In what will probably be a shock to many C# developers (and elicit snide remarks from the VB crowd), MVC 3 is embracing dynamic typing for controllers and view. The ViewBag property exposes a dynamic object upon which one can add properties at run time. Generally speaking this is used to transmit non-modal data from a controller to a view. The examples provided by Scott Guthrie include status text (e.g. the current time of day) and the items used to populate a list box.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Nice article by Mihai Lazar

I think Microsoft is a business. As a business they generate revenue by doing what the client asks. A rather strange breed of clients are developers. They always want ... something. What the 'developer' wanted was a type safe, almost as fast as C, but with a managed Garbage Collector. Microsoft delivered it in the form of the .NET ecosystem.

Now we want MVC, rails, cool tools :D. And they are delivering them.. It takes a while until the best strategy for generating revenue is decided upon, but you can usually bet you are going to get a great ROI. So if the developers like dynamic languages, and functional languages that I think that's what MS will deliver.

Just think when we'll want only modelling languages =)) :D.

Biased by Olmo del Corral

It's completely true that the MVC team is embracing some important changes in Microsoft, like Open Source or more open informal releases, but so far dynamic programming is not the most sucessfull feature in the language.

ViewBag is one of the very few dynamic members in the framework and is there 'not even' replacing the string -> object dictionary ViewData. So far, LINQ is a trend, dynamic is not.

In MVC 2 and 3 there's much more investment, however, in the new Expression-based helpers (EditFor, TextBoxFor...) that are completely static typed where before we had to use string for property names.

I want to see programmer choice by Mike Gale

There is a common belief that framework designers should force developers to do one thing OR another.

I say complete and utter rubbish. Let the programmer decide for herself.

The spirit of the article "Static Typing Where Possible Dynamic Typing When Needed: The end of the cold war between programming languages" expresses the attitude I favour pretty well.

I hope that programmers are not unquestioning mind slaves of others. Give a choice, get a life.

Re: Biased by Jonathan Allen

The reason I didn't talk about LINQ was that nothing really changed in that regard between MVC 2 and MVC 3.

But since we are on the topic, I really wish they would find a way to remove the need for lambdas in model binding. It is needlessly verbose in C#, and in VB it is just plain unbearable. I would much rather either have a XAML-like binding syntax or an extension to the Razor view engine that exposes a better way to create the abstract syntax tree.

Re: Biased by Vladimir Kelman

Is it possible to use F# there? Lambdas would be perfectly concise then :)

Re: Biased by Olmo del Corral

I aggree it would be usefull to have a even sorter synthax for lambdas, but i would make it general not Razor specific.

I propose that lambdas from one parameter that return, like "a=>a.Name", could be written just ".Name" this way we could even write queries like:

people.Where(.Nombre == 2).Select(.DateOfBirth);

Re: Biased by Jonathan Allen

That wouldn't work in the general sense, the compiler needs something to indicate you are creating a lambda. That would also make it impossible for C# to ever add With blocks, which I find to be ever so useful.

Re: Biased by Jonathan Allen

Good question. I just asked Scott Guthrie, I'll let you know if I get an answer.

Re: Biased by Andrew Marshall

My old friend,

You're famous!!! www.infoq.com/news/2011/01/Razor-Extensions

Andrew

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

9 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT