ViewModel tree and frequent model tree updates

In my WPF MVVM application, my model is a complex tree of Model objects that constantly changes at runtime. Instance instances come and go at runtime, change their position inside the tree, and, of course, change many of their properties. My view is an almost individual visual representation of this tree. Each instance of the model is in 80% of cases and node in the tree.

My question is, how will I create a ViewModel around this? My problem is that there are quite a few different types of models with each fairly large number of properties. If I understood MVVM correctly, the view should not directly communicate with the model, so that would mean that I would need to create a ViewModel type for each model type and rewrite each property of the Model type in the ViewModel.

Also, the ViewModel will need to "bind" to the properties of the model changes in order to pass them along with the view (using wpf datatbinding). I will need some factory that creates and injects a ViewModel instance for each model that reappears, and I would like to use every ViewModel instance when the corresponding model disappears. I end up tracing all the instances I created. It is incredible how much bloated code the fees for this double packaging are generated. Is this a really good approach? Each object and each property more ore less exists twice, and I have a lot of additional code that keeps the model and view in sync. How do you deal with this? Is there a smarter way to solve this problem?

Does anyone have a link / sample for this that does this better than me?

+4
source share
1 answer

I think you might run into a paradigm trap if you follow this path. MVVM is nothing more than a template that simplifies development in the WPF world. If this is not the case, do not use it or revise your approach. I would not spend 80% of my time to check the "Using MVVM" field.

Now back to your question. Correct me if I am wrong, but it looks like you are looking at MVVM in the opposite direction: you do not need a Model for ViewModel . Usually, you first create ViewModels based on your view, and then only on the model.

As a rule, you look at the screen layout from graphic designers and create the corresponding ViewModel, which takes all the necessary fields from the Model, wraps / modifies / formats / combines them to make viewing as simple as possible.

You said that your view is an almost individual visual representation of the Model. In this case, it may make sense to create a very simple ViewModel that provides the root object of your model tree and allows the View to consume the model directly through this property. Then, if you need some settings or view commands, you can delegate this ViewModel.

Sorry for the very vague answer. Maybe if you ask a more specific question, we can dispel the confusion :) ...

+9
source

All Articles