Add editing to MVVM in a hierarchical data structure

This question is a continuation of this older one , and it is more a confirmation than an open question.

In my ViewModel instance is a particular instance of the model _modelInst.
ViewModel has exclusive access to model data during editing (therefore, models do not need to implement INotifyPropertyChanged).

Now there are three ways I came up with how to edit model data from a view:

  • Getting / setting directly in the model
    instance for example for simple value fields
    return _modelInst.fieldname;
    _modelInst.fieldname = value;
    This easy to use ...

  • Creating an instance of ViewModel and working with the parent data structure,
    for example, for more complex types of objects, such as structs:

    • Create a new ViewModel for this type.
      ViewModel knows the parent and its field name.
    • maps this to ContentControl + DataTemplate
    • receiving / setting:
      through the methods of the parent with the fieldname parameter as a parameter,
      overwriting the entire source object, even if only one field is changed.

    This means that for each of these structures a new interface is created (with update procedures working on _modelInst) implemented by the parent.

  • Creating ViewModel instances without directly knowing the structure of the parent data,
    for example for (lists) of classes inside parent classes

    • ViewModel

      • ( ,
        )

      , .
      , ...

(4.) ViewModel, , subobject (+ , ).
.
_modelInst.

( ) ?
MVVM ()?
MVVM ?

+5
2

, ; , MVVM / :

+1

, , , MVC.

ViewModels , , , .

,

        -->Order

        -->Country

(, ), .

, , , viewmodel. , .

public class CustomerView: // () {

public CustomerView(Customer customer)
{
      this.FirstName = customer.FirstName
      //etc..

      //Only if you need it, that is if you have some display-specific
      //logic relating to country for a given view, you create
      //a CountryView class that inherits from Country and gets populated
      //by an instance of it as well
      this.CountryView = new CountryView(customer.Country)
}

public CountryView CountryView {get;set;} //sadly you cannot override Country but you may be able to shadow it.

public string DisplayColor
{
    if(base.FirstName == "Joe")
    {
        return "red";
    }
    return "";
}

}

, . - , .

0

All Articles