MVVM是否违反DRY? [英] Does MVVM violate DRY?

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

问题描述

我制作的ViewModel看起来可疑地类似于其他类,并且它们似乎需要大量的代码重复,例如在当前项目中,我有:

It seems that ViewModels that I make look suspiciously like other classes and they seem to require a lot of code repetition, e.g. in a current project I have:


  • SmartForm Model 代表数据填写的表格,具有以下属性:


    • IdCode

    • Title

    • 说明

    • SmartFormFields的集合

    • 等。

    • SmartForm: Model that represents a data form to fill in, has properties:
      • IdCode
      • Title
      • Description
      • collection of SmartFormFields
      • etc.

      • IdCode

      • 标题

      • 描述

      • SmartFormFields

      • 等。

      • IdCode
      • Title
      • Description
      • collection of SmartFormFields
      • etc.

      因此我的 ViewModel与我的模型基本相同,只是具有所有与视图绑定的OnPropertyChanged功能。

      So my ViewModel is basically the same as my Model, just with all the OnPropertyChanged features for binding with the View.

      将其扩展为我对模型所做的每一个小改变,我必须对ViewModel进行镜像更改

      It seems as I refactor and extend this that every little change I make to my model, I have to make a mirror change to the ViewModel.

      这似乎是违反了不要重复自己的基本模式规则。

      This seems to violate a basic rule of patterns Don't Repeat Yourself.

      我是错误地实现了MVVM模式,还是MVVM的固有特征是在Model和总是存在一对一的重复ViewModel?

      推荐答案

      由于模型和视图模型,我个人认为它不违反DRY(我更喜欢演讲者一词)并不指向相同的信息。例如,您的VM和M都具有Title属性,但是您的VM的Title属性也可以包含验证,而模型的Title属性可以假定有效性。

      I personally don't think it violates DRY since the model and view-model (I prefer the term presenter) don't point to the same information. For instance your VM and M both have a Title property, but your VM's Title property could also include validation, whereas your model's Title property could assume validity.

      VM可能包含模型的所有属性,也可能具有验证(例如,标题必须为非空白),数据依赖项,可绑定的UI特定属性(图标,颜色,画笔等),

      While it's true that The VM may contain all of the properties of the model, there is also the possibility of having validations (e.g. Title must be non-blank), data-dependencies, bindable UI-specific properties (icons, colors, brushes, etc.) which aren't part of the view.

      基本上,所有UI模式在声明方式上都具有类似的重复:即级联修改。尝试在不更改控制器的情况下在MVC中更改模型。

      Essentially all UI patterns have similar "duplication" in the way you state it: namely cascading modifications. Try changing a model in MVC without changing the controller.

      话虽如此,MVVM(或用于分离UI,逻辑和状态的任何UI模式)可能过于繁琐。对于简单的情况,例如您的示例。当逻辑变得不仅仅是状态传递时,分隔控制器/演示者/视图模型的值就会减小。

      That being said, MVVM (or any UI pattern designed to separate UI, logic, and state) can be overly tedious for simple cases such as your example. When logic becomes little more than state-pass through, the value separating the controller/presenter/view-model decreases.

      在您的特定情况下,如果确实没有VM不会显示的任何逻辑,验证或UI特定属性,并且不必持久化,序列化模型或使其与现有结构向后兼容(或在VM中添加这样做的逻辑是琐碎),我会强烈考虑将M和VM结合使用,以避免创建其唯一目的是获取/设置基础模型的属性的属性。

      In your specific case, if there really isn't any logic, validation, or UI specific properties that your VM isn't surfacing, and your model doesn't have to be persisted, serialized, or made backwards compatible with an existing structure (or adding the logic to do so in your VM is trivial), I would strongly consider combining the M and VM to avoid creating properties whose sole purpose is to get/set the underlying model's properties.

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

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