简体   繁体   English

在分层数据结构中向MVVM添加编辑

[英]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 . 我的ViewModel实例有一个Model的私有实例_modelInst
The ViewModel has exclusive access to the Model's data during editing (so the Model doesn't need to implement INotifyPropertyChanged). ViewModel在编辑期间可以独占访问Model的数据(因此Model不需要实现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 直接在Model实例上获取/设置
    eg 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 创建ViewModel实例并对父数据结构进行操作
    eg for more complex object types like structs : 例如,对于更复杂的对象类型,如结构

    • Creating a new ViewModel for that type. 为该类型创建新的ViewModel。
      The ViewModel knows the parent and its fieldname. ViewModel知道父级及其fieldname。
    • displaying that in a ContentControl+DataTemplate 在ContentControl + DataTemplate中显示它
    • getting / setting: 获取/设置:
      via methods of the parent with the fieldname as parameter, 通过使用fieldname作为参数的父方法,
      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. 这意味着为每个结构创建一个由父项实现的新接口(使用_modelInst上的更新例程)。

  3. Creating ViewModel instances with no direct knowledge of the parent's data structure 创建ViewModel实例,而不直接了解父数据结构
    eg for (lists of) classes within parent classes 例如,用于父类中的(列表)类

    • Creating a new ViewModel for each class 为每个类创建一个新的ViewModel

    • 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). (4.)可以创建一个通用的ViewModel,它只通过反射工作,其中每个子对象都知道它的父对象和它的字段名(+ index,如果在列表中)。
Only the root's logic would then interfere with the model. 只有root的逻辑才会干扰模型。
But that solution would also require a means to store the path to a field within _modelInst . 但是该解决方案还需要一种方法来存储_modelInst字段的路径。

Is there any other (more simple) way to achieve this? 有没有其他(更简单)的方法来实现这一目标?
Did I misunderstand the principles of MVVM (again)? 我是否(再次)误解了MVVM的原理?
Is MVVM suited for manipulation of large hierarchical data structures? MVVM是否适合处理大型分层数据结构?

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: 当我学习MVVM以及如何使用视图模型表示对象图/层次结构时,他们帮助了我很多:

  1. Editable Object Adapter 可编辑对象适配器
  2. Editable Collection Adapter 可编辑的集合适配器
  3. MicroModels 微观模型

This is an excellent question for which I do not feel there is a good answer that comes stock with the MVC pattern. 这是一个很好的问题,我觉得MVC模式没有一个好的答案。

ViewModels work great when the model they map to has no children. ViewModels映射到的模型没有子项时效果很好。

But when the model has children, as in 但是,当模型有孩子时,就像在

Customer 顾客

        -->Order

        -->Country

(imagining Country were a child object of Customer) the design pattern kind of breaks down. (想象国家是客户的儿童对象)设计模式的类型分解。

The best thing I've found is to use inheritance and selectively expose only those children for which you need viewmodel logic. 我发现最好的事情是使用继承并有选择地只暴露那些你需要viewmodel逻辑的子代。 Otherwise, just access the model's properties of the view that will come in via inheritance. 否则,只需访问将通过继承进入的视图的模型属性。

public class CustomerView : Customer //inherits from Customer (model) { 公共类CustomerView:客户//继承自Customer(model){

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 "";
}

} }

This gets messy when dealing with grandchildren. 与孙子女打交道时,这会变得混乱。 If anyone has a better solution, I would love to hear it. 如果有人有更好的解决方案,我很乐意听到。

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM