Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC and MVVM

MVVM is a Microsoft design pattern that existed before ASP.Net MVC. Can anyone through light on differences between MVVM and the new MVC pattern?.

like image 819
A_Var Avatar asked Sep 17 '10 22:09

A_Var


2 Answers

Can anyone throw light on differences between MVVM and the new MVC pattern?.

Yes: When using ASP.NET MVC the MVC pattern uses the controller to render the model directly into the view. This is perfectly acceptable for trivial projects with a small number of objects. Where this can become a problem is that the concerns of the UI layer can bleed through to the underlying (domain) model.

When using MVVM then you are adding an abstraction between the Model and the View, which is of course the ViewModel. This allows the author to project into the view an object that is most readily consumed by the view. The ViewModel can contain things which would be out of place in the (domain) Model. The cost associated here is that you need to have mapping logic which transposes the data from the model to the View Model. Tools like AutoMapper can assist with this chore.

A simple example of this might be the Model doesn't require certain fields as required, but a particular View does. Rather than baking this logic into the user interface, if it is attached to the ViewModel, then other UI's can consume the same VM without having to duplicate logic that was baked into the first user interface.

like image 135
James Fleming Avatar answered Sep 21 '22 22:09

James Fleming


MVC and MVVM are actually quite different. There seems to be a fair bit of misunderstanding of MVVM when talked about with ASP MVC. The practice of making 'View Models' in MVC, which are specific classes to feed views, while good practice is not true to the spirit of MVVM, and in fact is just a cleaner version of MVC.

MVVM is more suited to the desktop using WPF or similar, or purely in the browser using a JavaScript framework such as knockout.js. The pattern is quite different to MVC and involves views being 'subscribed' to the model.

like image 21
Paul Grimshaw Avatar answered Sep 21 '22 22:09

Paul Grimshaw