The following scenario came up in our project: We have hierarchical business objects. The root node is meant to be a project. The project contains different kind of data in it. This data is split up in "Static Data", "Result Data" and "Control Data".
Project
|
+--- Static Data
| |
| +---- Dataset 1
| |
| +---- Dataset 2
|
+--- Control Data
| |
| +---- Dataset 3
| |
| +---- Dataset 4
|
+--- Result Data
|
+---- Resultset
The application has a kind of Project Explorer (like Solution Explorer in VS) that shows the data structure as it is shown above in a tree view. To achieve that I create different view models with the corresponding models, e.g. ProjectViewModel, StaticDataViewModel and DatasetViewModel for the leaves. If the user clicks on one of the datasets a view opens that shows the data.
When I display the datasets in a other view, should I use the DatasetViewModel that is used in the tree, or should I create a new View Model (DatasetDetailsViewModel)?
Two things I read about the MVVM pattern: 1. The ViewModel should not be aware of how its data is displayed in the View. 2. The ViewModel is some kind of state machine.
Having those two points in mind I am not sure whether I could use the same ViewModel for tree view and details view. As on the one hand I could use the same view model, because it's just a different kind of displaying the dataset. The tree view is just showing the name of the dataset, whereas the detailed view shows the actual data that is contained. On the other hand, the view model is some kind of state machine, and the views may have states that do not fit together. For example if I have a visibility flag, I might set it to true for the treeview but in the detailed view I want to set to false.
So I am not sure if I should rather create a new view model or use the existing one from the treeview.
Opinions wanted!
Thanks, Florian