Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do we use MVVM?

I have been hearing a lot of hype about MVVM for WPF.
When do we use it?
Is it a use for everything or does it only have specific uses?
Is it worth it for every project?

like image 693
akshaykarthik Avatar asked Apr 08 '10 14:04

akshaykarthik


People also ask

When should MVVM be used?

MVVM is enough for small projects, but when your codebase becomes huge, your ViewModel s start bloating. Separating responsibilities becomes hard. MVVM with Clean Architecture is pretty good in such cases. It goes one step further in separating the responsibilities of your code base.

Where is MVVM used?

The pattern is often used in Windows and web graphics presentation software. The MVVM pattern is used in Windows Presentation Foundation (WPF), which runs on Microsoft's . NET. Silverlight, a Microsoft WPF internet equivalent multimedia plug-in, also uses MVVM.

Why we use MVVM instead of MVC?

MVVM separates the different components of the development process into three categories, model, view and ViewModel. This typically involves code markup or graphical user interfaces (GUI). MVC, or model-view-control is a way developers separate programs into these three components.

Where is MVVM and MVC used?

MVC patterns are found in popular frameworks such as Rails, Django, and CakePHP, while MVVM patterns are found in Silverlight, nRoute, Caliburn, and WPF.


2 Answers

It can be useful in any project, but I find it particularly helpful in situations where providing a clear separation between business logic, interaction logic, and user interface is required (large applications or applications involving multiple developers/designers).

Model = Business Logic

  • Contains the model of whatever business process/object I am working with.

ViewModel = Interaction Logic

  • All the code that controls how the model is accessed and modified (e.g. edit/undo functionality, lazy loading, etc.)

View = User Interface

  • The interface (defined in XAML) that the user interacts with. I try to minimize the use of code-behind in this layer, pushing that into Attached Properties or the ViewModel.

There are doubtless many other uses for MVVM, but this particular scenario is the one I have found to be the most useful in my own WPF development experience.

like image 117
Joseph Sturtevant Avatar answered Sep 18 '22 00:09

Joseph Sturtevant


I've found it useful even in relatively small projects, if I'm making a lot of use of databinding and an object data model / models.

like image 38
pete the pagan-gerbil Avatar answered Sep 20 '22 00:09

pete the pagan-gerbil