When I’m designing multiple views under the MVVM pattern, does each view get its own ViewModel or do they all share the same one? I understand that this is ultimately a flexible decision, but what is the best practice?
My gut tells me to have a ViewModel for each view (i.e. each separate UI window). All of the blog examples of MVVM show a single view but not much beyond that.
Yes, basically the idea is that your viewModel should only be used by one view. If you use a viewModel to populate an area (like in ASP.NET MVC) then that viewModel is “reused” each time that view is presented in difference places.
This article is a discussion by Josh Smith of the MVVM pattern. Then, Ward Bell discusses in this article what he thinks Josh left out, while maintaining that Josh’s work is still very strong.
Ward does an excellent job of laying out the complexities of this pattern and showing the tension that exists. Here is his take on the tension: