What are the advantages and disadvantages of View-first vs. ViewModel - first in the MVVM template

I give a presentation on the use of MVVM in real applications, and I include a section on constructive solutions to religious wars involving using MVVM as a template in your application. In the MVVM application, there are two main ways (which I know) to create a new View / ViewModel pair:

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

In your experience, what are the pros and cons of each method? What do they solve and what problems do each face?

Summary of Results




  • View First - Pros
    • It’s easy to keep track of which ViewModel is used in the view.
  • View First - Against
    • Does not allow to use one view with several ViewModels
    • An additional event is required to handle the relationship between Views and ViewModels.
  • ViewModel First - Profiles
    • Allows more complete testing of logic to open new views and ViewModels
    • DRYer tends to get more applications.
    • View and ViewModel are more independent and can work more easily.
  • ViewModel First - Cons
    • It is more difficult to configure in Silverlight without a DataTemplateSelector and print the DataTemplates.
+44
mvvm
Sep 21 '10 at 17:57
source share
6 answers

Given the Data Templating function in WPF, I believe ViewModel-First is the way that WPF was designed for .

I will clarify this statement: Data Templating allows you to never instantiate from ViewModel. If everything is done correctly, your Views and ViewModels can be stored in separate projects that DO NOT bind to each other. In addition, the ViewModel project doesn't even have to reference PresentationFramework assemblies, which makes your ViewModels available for consumption by any imaginable user.

+13
Oct 18
source share

I usually prefer the View-Model simply because I think it's best to follow the DRY rule. When you start building applications on a large scale, I find that this simplifies testing, thereby outweighing the initial bit of a headache that you need to solve when setting up the application.

+4
Sep 21 2018-10-21T00-09-21
source share

Caveat - I use WPF, not Silverlight.

Using a virtual machine that creates an instance of V (how I do it), the view is independent and can be used independently of the VM (for example, in the designer)

Personally, I turn to MVVMC (models, View, ViewModel, Controller), where I have a control class that creates instances of ViewModels and Views and "attaches them." Then C also processes the receipt of data (and caches it, etc.) and any communication through virtual machines and Vs (for example, if an instance of V is created, routes a command to its virtual machine to perform some action, the VM will probably ask C perform an action on his behalf, C can then raise the corresponding events that other VMs can handle

If (whether you use a controller or not), I need a virtual machine to talk to another virtual machine, it is harder to do if V creates an instance of the virtual machine - because I do not need to expose the virtual machine to V (or, at least make some interface available so that the second VM can talk to 1st).

+4
Sep 28 2018-10-12T00:
source share

At first we used ViewModel, but when I outsourced and using the mix became the most important, my boss said that View-first is better than Viewmodel-first - I disagree with it (but one of many is not the best vote ratio; - )), because now we have some strange connections with vision events in the code behind. Now I won’t return, and I am stuck in some user controls - due to changes.

+1
Sep 28 '10 at 5:13
source share

I use the View-first (sort-of) approach. I define View in collaboration with my client with a viewmodel with test data. When we are satisfied, I proceed to extract the interface from 'dummy' and implement the real ViewModel. I found this approach most attractive for the following reasons:

  • Fast, since prototyping is expensive in time, and I often do it correctly (ish) in my fourth or fifth attempt.
  • ViewModels tend to be lightweight (easier) to implement when I have an interface to stick with.

I work in WPF, but I would think that it would not be too different in SL. Also, I never spend time testing views that may be relevant to my choice of approach.

0
Sep 21 '10 at 18:10
source share

I prefer to use the first approach to the presentation model. For many reasons:

  • Vms is your application that contains most of the logic, in addition to glue code in the form of behavior or triggers.
  • If you create views, you are responsible for its life and cleanup code. You must deal with threads and other issues that are difficult to verify. On the other hand, you create vms and leave the logic of creating views using WPF through the data template. You do not need to worry about the problem of streaming. And there will be a better separation of problems.
  • With vm first reverse null code.
  • With project-level isolation for viewing and vms, you can restrict developers by using certain things, such as the dispatcher in the view model, leaving a cleaner and more testable code base. Ie view sprojec project before vm. And the vm project should not reference any presentation library.
  • If there is a clear boundary between the view and vm. Both can develop and be less fragile.
0
Feb 01 '16 at 22:48
source share



All Articles