Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confused with Model vs ViewModel

Tags:

I am Learning ASP.NET MVC and downloaded a couple of sample apps. MusicStore etc...

I am coming from a wpf background where we had the MVVM Pattern. I have noticed that they used the concept of model and ViewModel.

In MVVM is pretty clear that you bind the view to the ViewModel injecting the model into the viewModel. In MVC you have a controller but I am not sure and confused how the all ties together,as I cannot see the model injected into the ViewModel

I have the following structure

  1. MyCompany.Entities.dll (All the models go here) EG Product
  2. MyCompany.Dal.dll (All the repositories go here)
  3. MyCompany.Services.dll (called by MyCompany.WebUI.Controller calls MyCompany.Dal)
  4. MyCompany.WebUI.MyApp
  5. MyCompany.Tests

From some of the examples I have seen your Model acts as a ViewModel.Am I correct?

Let's take a controller i have something like

public class ProductController {     public ProductController(IProductRepository productRepository)     {         //omitted as not relevant     } } public class ProductVM {     public ProductVM()     {           // Shouldn't we inject the model here RG Product     } } 

Is there some N-tier examples out there I can refer to? Is the concept of ViewModel a valid one in MVC? What is the standard?

Thanks for any suggestions.

like image 216
user9969 Avatar asked May 31 '11 09:05

user9969


People also ask

What is difference between model and model and view?

A model is usually more closely related to how your data is stored (database, services, etc.) and the model will closely resemble those. The ViewModel on the other hand is closely related to how your data is presented to the user. It is usually a flatten version of your model, denormalized, etc.

Should ViewModel know about model?

Models are just the plain data, and a ViewModel is something that acts like a padding in between the two, that it should get information from the Model and pass it onto the View, and the View should know how to present it.

What is ViewModel and model?

Model–view–viewmodel (MVVM) is a software architectural pattern that facilitates the separation of the development of the graphical user interface (the view) – be it via a markup language or GUI code – from the development of the business logic or back-end logic (the model) so that the view is not dependent on any ...

What is model and ViewModel in MVC pattern?

What is ViewModel? ViewModel in the MVC design pattern is very similar to a "model". The major difference between "Model" and "ViewModel" is that we use a ViewModel only in rendering views. We put all our ViewModel classes in a "ViewModels" named folder, we create this folder. Understand it with an example.


2 Answers

Use ViewModels to simplify the View.

For instance, you might have a deep object graph with Products, Order, Customers, etc - and some information from each of these objects are required on a particular View.

A ViewModel provides a way to aggregate the information required for a View into a single object.

ViewModel's also allow for things like data annotations and validation - which does not belong on your model, as your model should stay "domain-specific".

But in reality, ViewModels are nothing more than a simple wrapper for your domain objects.

Use a tool like AutoMapper to map back and forth between your ViewModels and domain models with ease.

Personally i always bind to ViewModel's in my View's, never to the domain models, even if it's a single object. Why? Well i like to decorate my ViewModels with UIHints, validation, data annotations. Just the same way your domain models are enriched with domain-specific rules and business logic, so should your ViewModels be enriched with UI-specific logic.

If you simply have a object with a 1-1 representation of your domain model, you are missing the point of ViewModels.

Add to the ViewModels only, and nothing more, what is required for a particular View.

Example controller action

public ActionResult CustomerInfo(int customerId) {    // Fetch the customer from the Repository.    var customer = _repository.FindById(customerId);     // Map domain to ViewModel.    var model = Mapper.Map<Customer,CustomerViewModel>(customer);     // Return strongly-typed view.    return View(model); } 
like image 152
RPM1984 Avatar answered Sep 19 '22 09:09

RPM1984


The difference between MVC and MVVM is that MVC has one set of classes for the data entities. In MVVM you have 2 - one set for binding to your views, and one set for managing the data persistence (which could be in a separate WCF service for example).

The benefits of MVVM are that the model bound to the views is relevant to the UI and completely independant from the persistence Model.

Which to use? Well it depends on how closely the data structure required by your Views maps to the structure of the database. When it is similar - it is possible to bind the DataEntities from your DAL directly to your view - this is the classic MVC pattern. However, you gain much with a separate ViewModel as you can extend these classes with View specific behaviour (e.g. Validation) that your DAL should not be concerned with.

For all but the most simple applications, I would recommend use of a separate ViewModel.

like image 35
BonyT Avatar answered Sep 19 '22 09:09

BonyT