Question

In researching MVC 3 archetectures, one thing I've seen consistently is the concept of breaking the models out into their own project and calling it ViewModels.

As I'm coding, I'm having a hard time realizing what the benefit of this is. Could someone explain what the advantages of this are?

Was it helpful?

Solution

I'm not sure where you go thid idea that View models should be in their own project. That's typically not how it's done.

Often the business model is in a seperate project, as is the data model, but the view models are typically in the web project where they belong, since they're a part of the presentation layer.

OTHER TIPS

Because one major thing about MVC is seperation of concern. You should create a ViewModel that has only the data your View needs, no logic.

You can, but you don't need, a seperate project (for example: a Data Access Layer)

A ViewModel is just a simple POCO class (Plain Old CLR Object, a class that only has properties, no logic)

More Information

ViewModels are used to tailor a Model for the use of a particular View. When you want to pass specific information to the View, or retrieve particular information, then you would construct a ViewModel.

An example of a ViewModel would be:

public class PersonViewModel
{
    public Person NewPerson { get ; set ; }
    public Address NewAddress { get ; set ; }
    public SelectList Suffixes { get ; set ; }
}

If you had a view that created a new person, you could pass it this ViewModel class that is designed soley for the purpose of a new person View.

View models are a part of the UI layer so they should generally stay in the same project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top