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

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

问题描述

我试图理解模型视图模型关系,我一直在同样的问题碰撞。
假设我们只用一个领域的人类:

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

在下一阶段我是谁创造了​​present一个人的用户控制我的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的,并在模型中添加属性为姓名我endup非常相似,我的ViewModel类的额外层,我不会需要的东西。有什么办法让我的模型类,因为它是,但仍然可以在视图模型类里面的变化通知?如果没有其他方式比使用INotifyPropertyChanged的机制或任何类似将于为什么我需要虚拟机模型来实现?它只是为少数聚集模型类成将被提供给视图一个类的情况?

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?

我想我必须失去了在我的理解什么导致它按我描述了一个模型,同时使用View code-背后作为控制器会比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.

推荐答案

这是真的,在很多情况下的ViewModels通过寻找像code额外的层没有真正增加任何有价值的东西开始生活,但问题是,他们能的演变

Putting the viewmodel code into the model is not the best idea

It's true that viewmodels in many cases begin life by looking like extra layers of code that don't really add anything of value, but the point is that they can evolve.

一个虚拟机的作用是应用于意见提供便利的 - INotifyPropertyChanged的就是这样一个方便而模型的作用是封装业务逻辑的。当你的code的功能性的增长会越来越丰富;在某些时候,它甚至可能最终被在code尺寸比模型大得多。所以,区别是为了使便利性和业务逻辑部分由对方,这对你的code的可维护性很好的效果保持很好地分离的。

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.

在这里的原因是不同的:如果你做到这一点,那么就需要使用视图模型与其他类型来看,你将不得不复制所有逻辑到其他视图的codebehind为好。复制是坏的;干还是不错的。

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.

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

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