In a MVVM MDI app with singleton tool windows (think of Visual Studio), the tool window contents change according to current selected document. There are 5 tool windows now and more to come.
I can either reuse or create new View and/or ViewModel for each tool window per each document. My question is whether there is an overwhelming reason to choose or exclude one of these options? Is there another option I totally missed?
For each tool window:
-
Create a new View+ViewModel per document. When the user switches document, the tool window switches View+ViewModel. The memory cost is higher with this option but perf is good.
-
Create a ViewModel per document but reuse View. With MVVM pattern this is doable but UI re-layout can be expensive.
-
Reuse both View and ViewModel to minimize memory usage. Resetting ViewModel and loading another set of data could be hard to get right.
I ended up reusing
Viewbut notViewModeli.e. option 2.To answer the original question, option 3 should be excluded from consideration. Reusing VM is pointless – if the previous VM can be reused, it can as well be disposed and creating a new VM for the new data is much easier to do at a very ignorable mem/perf cost.
Option 1 vs. 2 depends on rendering complexity, performance goal and memory quota. In most cases the performance hit of switching data on the same UI should be acceptable. If the rendering really take as long as I need to save the view, there might be something wrong.