views:

54

answers:

1

In my understanding, the ViewModel pattern was designed to pass all the relevant data to View because 1) the view shouldn't perform any data retrieval or application logic and 2) it enables type-safety, compile-time checking, and editor intellisense within view templates.

Since dynamic expressions are defined at runtime, does this mean we don't get any of the 2)'s goodies?

+1  A: 

You do not lose any of the existing functionality. You can still have a strongly-typed view such that when you access the Model property it will be of your specified type. The only thing that is added is a shorter way of accessing items in the ViewData dictionary.

Instead of the following

ViewData["MyData"]

you can have

View.MyData

Notice that you do not lose any type-safety because you never really had any. In the former case the key is a string (no certainty that it exists in the dictionary) and the value is an object so unless you cast it you can't do that much with it. In the latter you also get no intellisense and the returned value must be cast to something useful.

In fact the implementation of View.MyData simply takes the property name ("MyData") and returns the value coming from the ViewData dictionary.

Arguably the one thing you lose is the ability to have spaces or other characters that are not legal C# identifiers in your key names.

marcind
Yes, this is what I was debating and this confirms my doubts.
randomguy