Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add editing to MVVM in a hierarchical data structure

This question is a follow-up of this older one, and it's more of a confirmation than an open question.

My ViewModel instance has a private instance of the Model, _modelInst.
The ViewModel has exclusive access to the Model's data during editing (so the Model doesn't need to implement INotifyPropertyChanged).

Now there are three ways I came up with how to edit the Model data from the View:

  1. Getting/setting directly on the Model instance
    e.g. for simple value fields
    return _modelInst.fieldname;
    _modelInst.fieldname = value;
    This one's easy to implement...

  2. Creating a ViewModel instance and operating on the parent's data structure
    e.g. for more complex object types like structs:

    • Creating a new ViewModel for that type.
      The ViewModel knows the parent and its fieldname.
    • displaying that in a ContentControl+DataTemplate
    • getting / setting:
      via methods of the parent with the fieldname as parameter,
      overwriting the whole original object even if only one field is changed

    This means creating a new interface (with update routines working on _modelInst), implemented by the parent, for each of these structures.

  3. Creating ViewModel instances with no direct knowledge of the parent's data structure
    e.g. for (lists of) classes within parent classes

    • Creating a new ViewModel for each class

    • Sending update instructions to the parent via

      1. commands
      2. messages
      3. reflection (parent knows which child called the function
        by comparing the instance to all stored children)

      All of these are a big mess implementing, creating functions for every field of the model that is editable.
      Which means pretty much all fields of the model...

(4.) One could create a generic ViewModel which works via reflection alone, where each subobject knows its parent and its fieldname (+index, if in a list).
Only the root's logic would then interfere with the model.
But that solution would also require a means to store the path to a field within _modelInst.

Is there any other (more simple) way to achieve this?
Did I misunderstand the principles of MVVM (again)?
Is MVVM suited for manipulation of large hierarchical data structures?

like image 512
Martin Hennings Avatar asked Nov 05 '22 10:11

Martin Hennings


1 Answers

Hopefully these resources will help; they helped me quite a bit as I learned MVVM and how to approach representing object graphs/hierarchies with view models:

  1. Editable Object Adapter
  2. Editable Collection Adapter
  3. MicroModels
like image 192
sellmeadog Avatar answered Nov 11 '22 08:11

sellmeadog