I am currently working on a WP8 application. My current design is to have each view bind against a specific view model directly.
Looking through the samples though, it seems that another way is to have all the view models accessed through the Main View Model and then have all the views to their view models through the MVM.
Is this the correct way to do it (So that it doesn’t cause flexibility and other issues in the future)?
1
Generally, yes, MVVM leads to a 1:1 binding between Views and ViewModels. It’s not a requirement, but seems to be a side-effect of how most people are writing MVVM structured applications.
It is possible to have one or more Views pointing to the same ViewModel. The VM merely needs to provide the properties that the specific View requires for the VM to be useful.
I would shy away from a hub-spoke pattern using a “main” VM that provides access to the other VMs. Sounds like more overhead than necessary without adding much benefit.
There’s an alternative pattern called MVPVM that’s a pretty useful read even if you’re going to stick with the MVVM pattern. MVPVM separates out the business logic from the VM and places it within the Presenter.
What I really liked about the article is it gives the progression of MVC -> MVP in the smalltalk days, then explains how MVP remained relevant for a lot of today’s UI’s and the natural progression into MVVM from MVP. The author gives a caveat about not discussing web applications, only desktop applications, but I think the article is broad enough to where you can glean the relevant details regardless of your target application.