views:

73

answers:

4

To me, this seems to make little sense, but after reading the information in the following:

http://weblogs.asp.net/scottgu/archive/2010/02/05/asp-net-mvc-2-release-candidate-2-now-available.aspx

http://bradwilson.typepad.com/blog/2010/01/input-validation-vs-model-validation-in-aspnet-mvc.html

http://blog.stevensanderson.com/2010/02/19/partial-validation-in-aspnet-mvc-2/#comment-35397( specifically some of the comments)

It appears that the idea behind Asp.Net MVC is that you have a one-to-one relationship between models and views. This seems to go against the DRY principle and several other standard programming practices.

For example, lets say you have a user account model and there are two views available to edit it - one for the user himself to edit it and one for the site admin to edit it. The admin has access to an additional field for something internal, required but the user cannot view/edit it. Per the model binding functionality and the beliefs described in the posts referenced above, I would need to create two separate user models, one for each page, and the only difference would be that additional field. This is just a simple example as well, I've got a few that I've seen where it would potentially mean 5 or 6 different models for the exact same object, just a few fields different between each view. That really doesn't make any sense to me.

+2  A: 

I did not read the posts you mentioned, but there is nothing wrong with having one Model for a couple of views.

I would just have this one UserModel and use it in all Views, even if there are some fields that are not used.

If things get a bit more complicated but Users still have a lot in common you can either use aggregation for the usermodel (User.Address) or use Interfaces (User has fields street , and city and implements IAddress).

Both methods have their pros and cons - with aggregation used in the majority of situations.

EDIT

After reading the posts I saw that they deal with validation. This is a different story. If you want to use DataAnotations you have to have different classes if validation varies. I dont use DataAnnotations - so I guess your class design might be different.

Malcolm Frexner
Yeah, they do talk about validation, which is how I came across them. However, they do talk about the reason for validation being done that way is because the community wanted it done that way because views should match up with models exactly, which doesn't make sense unless you are doing view models, which is NOT MVC. It may be a subset of MVC, but it is not MVC in the purest sense.
Charles Boyung
A: 

There is no official requirement to have only one view per model in ASP.NET MVC. In many cases that would lead to duplication of code.

I personally like to split model-view dependencies, that is, one view per model. It comes down to the fact that you never know how, say, a couple of very similar model-view pairs are going to evolve in the future. If they're separate, you just introduce changes in one and you don't have to "fix" the other views that were dependent on this model, or worse, to take extra work to create own models for them all at once.

Developer Art
Why was this voted down?
Robaticus
+1  A: 

If you're using annotations, I'd strongly consider one "model" and multiple "viewmodels." We went with a viewmodel approach on our current app and have been reaping the benefits, because our basic model needs to be shown in a couple different views.

Robaticus
But if you have different versions of a user you still use different classes that have different DataAnnotations?
Malcolm Frexner
There is one viewmodel per view. The viewmodel contains all of the annotations for that particular security model.
Robaticus
A: 

TL;DR: Make many view models. They are cheap and flexible.

"This seems to go against the DRY principle and several other standard programming practices."

[Citation Needed]?

MVC doesn't change the fact that in any language or pattern you need to make a view model definition for each separate screen. Whether via attributes, via XML, via toggling web form controls, whatever.

The DRY principal usually pertains to repeating business logic. Repeating a FirstName property across a CRUD screen section really isn't a big deal. Even 5-6 times, whats that? 40 seconds?

If you mistake your view models for object oriented classes and not homoiconisticish screen representations you run the risk of filling them up will all sorts of inheritance and or business logic.

Your not really programming when you make dumb view definitions. This work could easily be done in an Access GUI or defined in XML. The fact that your screen-view-models are in C# just makes it easier to fill them up with data and ship them around and work with tools like WCF and Automapper.

jfar
You are talking about view models, NOT models. I'm talking purely about a pure model representation.
Charles Boyung
-1 I still think that it is duplicate code. I see the reason for it, but I disagree on that it will not be a problem at some point in the future.
Malcolm Frexner
@Charles Boyung - I know? Your not talking about models, your talking about view models. Models come from your domain if DDDing or Database if using active record or similar. You can't avoid having a different model for each business entity.
jfar
@jfar - I really want to respond to your comment, but I've read it several times now, and have no idea what point you are trying to get across.
Charles Boyung
@Malcolm Frexner - Yeah, but how big a problem? Your most common properties, address, price, firstname aren't going to change that often. Think about what we did in webforms days? You certainly had duplicate FirstName textboxes for each of your views. The only difference between those definitions and view model definitions is the format they are in. We were all ok with duplicate code then.
jfar