开发者

MVC 3 (Preview 1) Dynamic ViewModel

开发者 https://www.devze.com 2023-01-09 10:10 出处:网络
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.

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? I开发者_StackOverflow中文版s this a sensible use of the dynamic type?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号