ViewModel 与 Model 之间的 MVVM 设计模式关系 [英] MVVM design pattern relation between ViewModel and Model

查看:38
本文介绍了ViewModel 与 Model 之间的 MVVM 设计模式关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于MSDN的图片

似乎所有的数据和业务逻辑都应该在 Model 内部,其中 View Model 应该有一组重复的 Model 属性用于显示目的.并且 View 应该绑定到 ViewModel 内部的重复属性,而不是直接绑定到 Models 内部的属性.

It seems like all the data and business logic should be inside Model where View Model should have a duplicated set of properties of the Model for display purposes. And View should bind to the duplicated property inside the ViewModel instead of binding to the properties inside Models directly.

ViewModel 应该实现 INotifyPropertyChanged 接口,以便让 View 知道某些属性是否发生更改.

ViewModel should implements INotifyPropertyChanged interface to let View know if certain property is changed.

但是 Model 应该如何通知 ViewModel 有关更改的信息?它也应该实现 INotifyPropertyChanged 吗?如果是这样,那么我们可以直接将 View 绑定到 Model 的属性.中间有一个额外的层并且我们必须手动处理所有数据更改通知的真正好处是什么?

But how should Model notify ViewModel about changes? Should it implement INotifyPropertyChanged as well? If so then we could just have the View bind to the Model's property directly. Whats the real benefit of having an extra layer in between and we have to manually handle all the data changed notifications?

根据我的理解举例:
查看:

example based on my understanding:
View:

<Grid>
    <TextBlock Text="{Binding foo}"/>
    <Label Content="{Binding bar}"/>
</Grid>

查看模型:

class ViewModel : INotifyPropertyChanged
{
    Model _m;
    public ViewModel(Model m)
    {
        _m = m;
    }

    public string foo
    {
        get
        {
            return _m.foo;
        }
        set
        {
            _m.UpdateFoo(value);
            //This one works fine. xaml will call getter to get the dead beef version
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("foo"));             
        }
    }

    public string bar
    {
        get
        {
            return _m.bar;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

型号:

class Model
{
    public string foo { get; private set; }
    public string bar { get; private set; }
    public void UpdateFoo(string newVal)
    {
        foo = newVal + "dead beef";
        bar = newVal; //how do i tell ViewModel that i have changed?
    }
}

推荐答案

通知可以通过INotifyPropertyChanged来自模型;但实际上,手动使用该界面可怕.必须根据已更改属性的名称来构建逻辑并不有趣.

Notifications can come from the model via INotifyPropertyChanged; but realistically that interface is horrible to consume manually. Having to base your logic based off the name of the changed property is not fun.

带有通知的模型层可能类似于消息总线客户端,因为消息传入它,它会解析它并将相关(和强类型)事件发送到视图模型.然后,视图模型更新其引发 PropertyChanged 的​​数据对象的属性.

A model layer with notifications could be something like a message bus client, as messages come in it parses it and sends relevant (and strongly typed) events to the view model. The view model then updates properties on its data objects that raise PropertyChanged.

对于您更大的问题:您是否必须拥有单独的 ViewModel 和 Model 数据对象?

To your bigger question: Do you have to have separate ViewModel and Model data objects?

如果你想成为一个纯粹主义者,当然可以;复制您的对象.如果您想要一种合理的方法,只有在需要添加不适合(或不能存在于)模型对象的属性时才使用特殊的视图模型对象.

If you want to be a purist, sure; duplicate your objects. If you want a rational approach, only have special view model objects if you need to add properties that wouldn't be appropriate (or just cant exist on) a model object.

该模型更多的是关注点分离,而不是一组无用的重复对象.在前面的示例中,ViewModel 不应该关心对象或事件是否来自消息总线,它只知道如何为视图设置对象.模型处理作为消息总线客户端的实现细节.

The model is much more about separation of concerns than a useless set of duplicate objects. In the previous example, the ViewModel should not care that the objects or events came from a message bus, it just knows how to set up the objects for the view. The Model handles the implementation detail of being a message bus client.

这篇关于ViewModel 与 Model 之间的 MVVM 设计模式关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆