Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the pros and cons of View-first vs. ViewModel-first in the MVVM pattern [closed]

Tags:

mvvm

I'm giving a presentation on using MVVM in real world applications and I'm including a section on the religious wars design decisions involved when using MVVM as a pattern in your application. In an MVVM application there are two main ways (that I know of) to instantiate a new View/ViewModel pair:

  1. View-First in which you create a view and it creates its own ViewModel and sets it to its DataContext.
  2. ViewModel-First in which you create new view models and create new views in response to changes in ViewModel properties, usually with ItemsControls and/or DataTemplates.

In your experience what are the pros and cons of each method? What do they enable and what problems do you run into with each?

Result Summary


  • View First - Pros
    • Easy to track which ViewModel is used by a View
  • View First - Cons
    • Doesn't allow a single View to be easily used with multiple ViewModels
    • Requires extra events to handle communication between Views and ViewModels
  • ViewModel First - Pros
    • Allows more complete testing of logic to open new Views and ViewModels
    • Tends to be DRYer as applications get larger
    • View and ViewModel are more independent and can be worked on separately more easily
  • ViewModel First - Cons
    • More difficult to set up in Silverlight without DataTemplateSelector and typed DataTemplates.
like image 327
Bryan Anderson Avatar asked Sep 21 '10 17:09

Bryan Anderson


People also ask

Why do we need model-view-ViewModel pattern MVVM pattern?

It helps to improve the separation of the business and presentation layers without any direct communication between each other. The designer and developer can work together without interrupting each other. Easy testability and maintainability is also popular in MVVM Design Pattern.

What is the use of ViewModel in MVVM?

The purpose of ViewModel is to encapsulate the data for a UI controller to let the data survive configuration changes. For information about how to load, persist, and manage data across configuration changes, see Saving UI States.

What are the benefits of MVVM design pattern?

In Android, MVC refers to the default pattern where an Activity acts as a controller and XML files are views. MVVM treats both Activity classes and XML files as views, and ViewModel classes are where you write your business logic. It completely separates an app's UI from its logic.


2 Answers

Given the Data Templating feature in WPF, I feel ViewModel-First is the way WPF was intended to be used.

I'll clarify that statement: Data Templating allows you to never instantiate views from your ViewModel. If done correctly, your Views and ViewModels could be kept in seperate projects that DO NOT reference each other. Furthermore, the ViewModel project should not even reference any PresentationFramework assemblies, making your ViewModels consumable by any conceivable user.

like image 154
Andre Luus Avatar answered Sep 28 '22 01:09

Andre Luus


I tend to prefer the View-Model first simply because I feel it follows the DRY rule best. When you start creating larger scale applications I find this makes testing easier as well, thus outweighing the initial bit of headache you need to deal with when setting up the application.

like image 29
Kavet Kerek Avatar answered Sep 28 '22 00:09

Kavet Kerek