什么是MVVM模式呢? [英] What is the model in MVVM for?

查看:191
本文介绍了什么是MVVM模式呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了几篇文章,教程和博客有关MVVM模式。然而,有一件事我不明白。以三个层次:

I have read several articles, tutorials and blog posts about the MVVM pattern. However there is one thing I don't understand. Taking the three "layers":


  • 模型

  • 查看

  • 视图模型

据我已经明白MVVM模型中包含的原始数据,如在学生类的情况下,名称和地址。视图模型公开属性,这代表了模型数据视图。

As far as I have understood MVVM the model contains the "raw" data, e.g. a name and address in case of a Student class. The view model exposes properties to the view which represent data of the model.

在视图模型属性示例

public string Name {
 get { return model.Name; }
 set { model.Name = value; }
}

示例为模型

private string name;

public string Name {
 get { return name; }
 set { name = value; }
}

这听起来有点傻,但不这样创建一个冗余?为什么我必须保持名称的模型和视图模型?为什么一个不处理上完全视图模型的名称?

This might sound a bit stupid but doesn't this create a redundancy? Why do I have to keep the name in the model and in the view model? Why should one not handle the name on the view model completely?

推荐答案

在这样一个简单的例子,这个答案是肯定的(这是不合理的冗余)。但是,据推测,一个页面将包含比只是一个单一的Model对象多。你可能有页面的状态以及它都必须被跟踪多个其他模型对象。这是在视图模型完成的。

In such a simple example, this answer would be yes (it is unreasonably redundant). But, presumably, a page will contain more than just a single Model object. You may have the page state as well as multiple other Model objects which must all be tracked. This is done in the ViewModel.

例如,你可能有有关记录在状态栏显示的用户,以及其他信息作为服务运行检测更改到一个文本文件中。

For example, you may have additional information about the logged in user displayed in a status bar, as well as a service running to detect changes to a text file.

您还可能有编辑Student对象的形式。如果您想验证这些变化,那么你就不会想直接编辑Student对象,直到修改已经被核实之后。该视图模型可以用作在这样的情况下,临时存储位置

You may also have a form for editing the Student object. If you intend to validate those changes, then you wouldn't want to directly edit the Student object until after the modifications have been verified. The ViewModel can act as a temporary storage location in such a case.

<青霉>注意上述它在发生验证的情况并不少见模型,但即使如此,你可能会希望用户能够输入无效值,而在编辑表单的过程。例如,如果你的模型不允许在现场零长度值,你仍然想使您的用户删除的价值,移动到另一个字段(比方说,例如,复制),然后返回到现场,并完成编辑(粘贴)。如果您是直接关系到模型,那么你的验证逻辑可能不处理这种介于两者之间,还未​​结束状态,只要你愿意。例如,您可能不希望验证错误搭话你的用户,直到他们已经完成,并点击保存。

Note on the above: It is not uncommon for validation to occur in the Model, but even then you will probably want the user to be able to enter invalid values while in the process of editing a form. For example, if your Model does not allow a zero-length value in a field, you still want to enable your user to delete the value, move to another field (say, for example, to copy it) then return to the field and finish editing (paste). If you are tied directly to the Model, then your validation logic may not handle this "in-between", "not-yet-finished" state as you'd like. For example, you might not want to accost your user with validation errors until they've finished and clicked 'Save'.

您还可能有司令部的对象视图模型处理按钮点击等等。这将是将是一个型号无用特定域的对象。

You will also probably have Command objects in the ViewModel to handle button clicks and the like. These would be domain-specific objects that would be useless in a Model.

的ViewModels也很有用,当你需要过滤或在某种程度上暂时修改模型对象得到的东西在屏幕上是有用的。例如,您可能希望在一个系统与它们之间的十大表演的实时列表一起显示所有用户的列表(每10秒更新一次)。或者你可能想显示报告的列表,并呈现出整体的使用率等过滤,排序和定制数据将采取视图模型内发生的图形。

ViewModels are also useful when you need to filter or somehow temporarily "modify" Model objects to get something useful on the screen. For example, you may want to display a list of all the Users in a system along with a real-time list of the top ten performers among them (updated every 10 seconds). Or you may want to show a list of Reports and a graph showing the overall usage rate, etc. Filtering, sorting and customizing that data would take place within the ViewModel.

示范,另一方面,通常是尽可能纯。理想情况下,你只想要波苏斯的是(通常)模式到底是什么在你的持久性存储(数据库,或你有什么)。如果你的持久化存储有名字和姓氏字段,然后让你会模式。只有在你的视图模型,你会结合他们得到一个名称字段(无论是第一个最后或姓氏,名字视观的需要)

The Model, on the other hand, is typically as pure as possible. Ideally, you want only POCOs that (usually) model exactly what's in your persistent storage (database, or what have you). If your persistent storage has FirstName and LastName fields, then so would your Model. Only in your ViewModel would you combine them to get a Name field (either "First Last" or "Last, First" depending on the View's needs).

例如:

namespace Model
{
    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class Class
    {
        public string Name { get; set; }
        public float Score { get; set; }
    }
}

namespace ViewModel
{
    public class EditStudentRecordViewModel
    {
        private Model.Student _student;
        private IEnumerable<Model.Class> _studentClasses;

        /* Bind your View to these fields: */
        public string FullName
        {
            return _student.LastName + ", " + _student.FirstName;
        }
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public IEnumerable<Model.Class> PassingClasses
        {
            get
            {
                return _studentClasses.Where( c => c.Score >= 78 );
            }
        }

        public IEnumerable<Model.Class> FailingClasses
        {
            get
            {
                return _studentClasses.Where( c => c.Score < 78 );
            }
        }

        public void Save()
        {
            List<string> l_validationErrors = new List<string>();
            if ( string.IsNullOrEmpty( this.FirstName ) )
                l_validationErrors.Add( "First Name must not be empty." );
            if ( string.IsNullOrEmpty( this.LastName ) )
                l_validationErrors.Add( "Last Name must not be empty." );

            if ( l_validationErrors.Any() )
                return;

            _student.FirstName = this.FirstName;
            _student.LastName = this.LastName;
            Model.Utilities.SaveStudent( _student );
        }
    }
}

这篇关于什么是MVVM模式呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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