MVVM 在 ViewModel 中复制模型属性 [英] MVVM duplicating Model properties in ViewModel

查看:44
本文介绍了MVVM 在 ViewModel 中复制模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有一个指导,即模型不应将其实体暴露给 View,并且所有必需的属性都应在 ViewModel 中重复

It seems that there is a guidance that a model should not expose its entities to View, and that all required properties should be duplicated in ViewModel

示例:

Product
Id {get; set;}
Name {get; set;}
.......


ProductViewModel : ViewModelBase
Id {get; set;}
Name {get; set;}
.......

为什么需要这样做?如果 Model 没有实现 INPC,我可以理解这一点,但如果实现了,那么我觉得这完全没有必要.

Why is this required? I can understand this if Model doesnt implement INPC, but if it does, then I find this quite unnecessary.

推荐答案

当 View 绑定到 Model 时:

When the View is bound to the Model:

  • 如果视图需要更改或者您有多个视图,对模型的更改将导致绑定到该模型的所有视图发生更改.

  • If the View needs to change or you have multiple views a change to the Model will cause changes to all the Views bound to that Model.

从视图的角度来看,它绑定的对象可能不是那么直观;当您需要向对象添加属性和/或命令时,您是将它们添加到 ViewModel 并保留模型中的原始"属性还是修改模型?

From the View's perspective, the object it is bound to might not be that intuitive; when you need to add a properties and/or commands to the the object do you add those to the ViewModel and keep the 'original' properties in the Model or do you modify the Model?

拥有 ViewModel 可为您提供单个模型和多个(版本的)视图之间的额外抽象

Having a ViewModel gives you an extra abstraction between a single Model and multiple (versions of) Views

总而言之,这只是一个指南,但请注意,当您需要修改/更新应用程序时,今天看起来不错的方法可能不太好.

All in all it is just a guideline but be aware that what seems OK today might be not so great when you need to modify/update the application.

这篇关于MVVM 在 ViewModel 中复制模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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