开发者

ASP.NET MVC: having one model for all views

开发者 https://www.devze.com 2023-01-17 11:08 出处:网络
I like the way model binding in django works: you send stuff that you need in a Json-like way: \'param1\': val,

I like the way model binding in django works: you send stuff that you need in a Json-like way:

'param1': val,
'param2': val

In ASP.NET MVC you can't do it primarily because C# is a static language. You can use ViewData but it's ugly and not recommended.

So I had to create dozens of ViewModels for every view: IndexViewModel, AboutViewModel, etc. And today I got an idea: why not just create one Model class and have Visual Studio generate all necessary fields? It's almost like in django.

return View(new Model
            {
                Param1 = "asd",
                Param2 = "sdf"
            });

Param1 & Param2 are not members of the Model class, but V开发者_C百科isual Studio will automatically generate them.

Now my question is, is it gonna work? The thing is that there will be a lot of fields in this Model class. And when we pass it to a view, only a small percentage of fields made for that view in particular will be used. Is it gonna be bad performance wise?

Thanks


If you have all these properties on your ViewModel which aren't used (i.e null or whatever) it isn't really going to impact performance. What it will impact however, is your design.

Generally speaking, one model to rule them all as you are proposing is a bit evil and goes against separation of concerns. ViewModel's should be very simple and should be tailored to the view. ViewModel's shouldn't really provide the view with more or less data than what the view needs in order to render.

Consider this....

You have a generic model with 15 properties on it and you only set a handful of them. Somebody else designs a new view and looks at the model, they may not know which of those properties are sent and under what conditions they are set. Consequently they may be attempting to display data that does not exist. This isn't a very clean approach.

I would stick to individual view models and where there is common functionality between views, create an abstraction or base ViewModel from which other view models can extend.

Edit: One other thing you could do is use the new MVC 3 (still in preview) syntax (dynamic) for setting ViewData properties directly as if they were properties.

So rather than doing

ViewData["FirstName"] = "Bob";

You can do

ViewModel.FirstName = "Bob";

This gives you dynamic variables automatically in MVC 3.


Have a look a Clay the 'malleable' object model being used by the Orchard Project.

http://weblogs.asp.net/bleroy/archive/2010/08/16/clay-malleable-c-dynamic-objects-part-1-why-we-need-it.aspx

http://weblogs.asp.net/bleroy/archive/2010/08/18/clay-malleable-c-dynamic-objects-part-2.aspx


There's no reason it shouldn't work.

You can even skip the Model class and create an anonymous type with just the members you need.

return View(new { Amount = 108, Message = "Hello" });

The problem using anonymous types is that you're giving up on auto-completion in the view since you won't be able to type the view according to the model.


Just use dynamic

0

精彩评论

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