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:
- 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 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.