如何正确定义 Model 到 ViewModel 的关系? [英] how to correctly define Model to ViewModel relation?

查看:39
本文介绍了如何正确定义 Model 到 ViewModel 的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解 Model 到 ViewModel 的关系,但我一直在遇到同样的问题.假设我们有一个只有一个字段的Person"类:

I trying to understand Model to ViewModel relation and I keep bumping in the same problem. Assuming we have a "Person" class with just one field:

public class Person
{
   public string Name;
}

在下一阶段,我让我的 XAML 艺术家创建了一个呈现人的用户控件,他根据 MVVM 的实践需要将他的视图与 VMPerson(视图模型人)相关联,所以现在我添加另一个类:

In the next phase I have my XAML artist who created a user control that present a Person and he according to practice of MVVM need to relate his view to VMPerson (View-Model Person), so now I add another class:

public class VMPerson : INotifyPropertyChange
{
   private Person person;

   public VMPerson():this(new Person()){}

   public VMPerson(Person person)
   {
      this.person = person;
   }

   public Name
   { get { return person.name; }
   { set { person.name = value; PropertyChange("Name"); }
}

所以现在我几乎所有东西都设置好了..但是我的 VM 类与模型类的变化有什么关系??如果我添加 INotifyPropertyChanged 并在模型中为Name"添加一个属性,我最终会得到与我的 ViewModel 类非常相似的额外层,我不需要它.有什么方法可以保持我的 Model 类的原样,并且仍然会收到视图模型类中内部更改的通知?如果除了使用 INotifyPropertyChanged 机制或任何类似的将在模型中实现的方法之外别无他法,为什么我需要 VM ?是否仅适用于将少数模型"类聚合为一个将提供给 View 的类的情况?

So now I have almost everything setup.. but how would my VM class relate to changes in my model class?? if I add INotifyPropertyChanged and add a property for "Name" in the model I endup with something very similar to my ViewModel class an extra layer which I will not need. Is there any way to keep my Model class as it is and still be notified on changes inside it in the view-model class? if there is no other way than using INotifyPropertyChanged mechanism or anything similar which will be implemented in the model why do I need VM ?? is it just for a situation that aggregate few "model" classes into one class which will be served to the View?

我认为我的理解中一定遗漏了一些东西,因为根据我所描述的,在我看来,使用视图代码隐藏作为控制器时的模型到视图模式会比 MVVM 更好的抽象,但我当然不是确定.有人可以向我解释我缺少什么吗?谢谢.

I think I must be missing something in my understanding cause it seems to me according to what I described that a Model to View pattern while using the View code-behind as a controller would be better abstraction than MVVM, but I certainly not sure about that. Can someone explain to me what I am missing? thanks.

推荐答案

把viewmodel代码放到model里不是最好的主意

确实,在许多情况下,视图模型看起来像是没有真正增加任何价值的额外代码层,但关键是它们可以进化.

VM 的作用是为视图提供便利 -- INotifyPropertyChanged 就是这样一种便利,而模型的作用是封装您的业务逻辑.随着代码功能的增长,它会变得越来越丰富;在某些时候,它甚至可能最终的代码大小比您的模型大得多.所以区分是为了方便和业务逻辑部分很好地相互分离,这对你的代码的可维护性有很好的影响.

The role of a VM is to provide convenience to views -- INotifyPropertyChanged is one such convenience while the role of a model is to encapsulate your business logic. As the functionality of your code grows the will keep getting richer; at some point, it might even end up being much larger in code size than your model. So the distinction is made in order that the convenience and the business logic parts are kept nicely separated from each other, which has a good effect on the maintainability of your code.

这里的原因不同:如果你这样做,然后需要将视图模型与另一种类型的视图一起使用,你将不得不将所有逻辑也复制到另一个视图的代码隐藏中.重复是不好的;DRY 很好.

The reason here is different: if you do this and then need to use the viewmodel with another type of view, you will have to duplicate all the logic into the other view's codebehind as well. Duplication is bad; DRY is good.

考虑到 MVVM 的一个非常重要的特性是它提供了可测试性,而可测试性必然意味着至少有两种类型的视图(真实的和模拟的),这种方法的必要性变得显而易见.

Considering that a very important feature of MVVM is that it affords testability and that testability necessarily means at least two types of views (the real one and the mock), the necessity of this approach becomes evident.

这篇关于如何正确定义 Model 到 ViewModel 的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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