views:

171

answers:

2

I've just taken a look at the new features available in the MVC 3 preview: ScottGu's Blog. There are a lot of good improvements and it's well worth a read to see where MVC is going.

The thing that struck me was the addition of a dynamic type ViewModel. The point being that you would not need to reference view data using ViewModel["Message"] but could use ViewModel.Message.

What are people's thoughts on this? I'm struggling to see why dynamic types should be used in this way. It almost gives a false sense of security for developers to come across a member call as they would implicitly believe it to be strongly-typed. At least when you see an indexer with a "magic-string" you are aware of the possibility of a run-time error.

What are other people's thoughts on this? Is this a sensible use of the dynamic type?

+2  A: 

Your point about at lease knowing when to expect a compile time error is a good one but I would still rather the cleaner code with the better readability over the predictability of an error.

I really dislike the whole ViewData["string"] interface, it feels dirty.

This feels slick. So yes it is subjective and more about a feel than anything real, but when I read the Gu's post it was one of the more exciting aspects IMHO.

Sruly
+2  A: 

As long as in the view you are only using the string rendering for these dynamic properties, I don't see a real issue. If you're using it for anything other than something that will be simply rendered as a string, though, I think you should still go ahead and create a strongly-typed model. FWIW, I've completely gone away from using ViewData magic strings for anything other than "messages."

Just my opinion. Also, subject to change as I gain more experience with the new version.

tvanfosson
That's made me think further then. Would `ViewData.MyClass.MyProperty` work?
David Neale
@David, this seems to imply that it would http://blogs.msdn.com/b/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx
BuildStarted