MVVM 标准化 [英] MVVM standardization

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

问题描述

Silverlight 中有人发布 MVVM 目前缺乏标准化,因此每个人都有自己的味道..

Someone in Silverlight posted that MVVM currently lacks standardization so that everyone has own flavor..

这就是为什么我和 WPF Disciples 的一些人正在积极讨论每个人都同意的 MVVM 元素.我完全理解我们以不同的方式实现了模式,我们混合了几种模式或根据我们项目的需要或让开发人员的生活更轻松创建我们自己的模式..但是忘记那些困难或项目的特殊需要.我们来讨论一下大家都同意的MVVM模式的标准规则.我也发布了我的一些想法.

That's why me and a few guys from WPF Disciples are actively discussing which elements of MVVM that everyone agreed. I totally understand that we have implemented the pattern in different ways and we mixed the several patterns or create our own pattern based on our project's need or to make the developers' life easier.. But forget about those difficulties or the special need of your project. Let's discuss about the standard rules of MVVM pattern that everyone agreed. I posted some of my thoughts here as well.

为什么是 MVVM?

  • Testabiltiy(ViewModel 比代码隐藏或事件驱动代码更容易进行单元测试)
  • 明确区分用户体验设计师和开发人员
  • 增加视图的可混合性"
  • 模型永远不需要更改以支持视图的更改
  • ViewModel 很少需要更改以支持对视图的更改
  • 没有重复的代码来更新视图

在视图中做和不做

  • 不应该包含任何要测试的逻辑:正如格伦所说,MVVM 不是代码计数练习,我们可以在代码隐藏中编写代码.但是您永远不应该编写任何要测试的逻辑.例如:如果用户选择一个国家,那么您希望在您的视图中显示州或城市列表.这是业务需求,因此您应该进行单元测试来测试此逻辑.因此,您不应该在代码隐藏中编写它.
  • 可以是控件或数据模板
  • 使视图尽可能简单.:我们仍然可以小心地在 XAML 中使用数据触发器或值转换器或视觉状态或混合行为.
  • 如果某些内容不可绑定,请使用附加属性:

在 ViewModel 中做与不做

  • 视图和模型之间的连接器
  • 保持视图状态,值转换(您可以创建要在 ViewModel 中显示的数据结构,而不是使用 ValueConverter.例如:您需要显示 Name 而不是 First Name 和 Last name.您的 Model 可以有 First姓名和姓氏,但您可以在 ViewModel 中创建 Name 属性.)
  • 没有强或弱(通过接口)视图的引用
  • 使 VM 尽可能可测试(例如,不调用 Singleton 类)
  • VM 中没有与控制相关的东西(因为如果您要更改视图,那么您也必须更改 VM.)

模型

  • 可以是数据模型、DTO、POCO、域类和UI模型的自动生成代理,具体取决于您希望如何在领域服务和表示层之间进行分离
  • 未引用 ViewModel

您对此有什么建议或意见吗?

Do you have any suggestion or comment for that?

我们小组中有一个分歧.有人说ViewModel里面有View的接口就可以了.但有人说,如果View Model有View的接口,那就是MVP模式.

We have one disagreement in our group. Some said that it's okay to have the interface of View in ViewModel. But some said that if View Model has the interface of View then it will be MVP pattern.

我们的一位 MVVM 专家谈到 MVVM 与 MVP

One of our MVVM experts say about MVVM Vs MVP

视图 => 视图模型

  • MVVM 视图直接绑定到 ViewModel 并通过数据绑定与 VM 对话
  • 在 MVP 中,视图绑定到悬挂在 SupervisingController 上的模型或根本不绑定(被动视图).

ViewModel => 视图

MVVM

  1. INPC/属性绑定
  2. 活动
  3. 消息(事件聚合器/Messenger/RX 框架)
  4. 通过服务等中介
  5. 通过接口
  6. 通过委托(View 将委托传递给 VM,它可以使用它来回调它.例如,VM 可能公开一个 SetActions 方法,View 调用该方法传递它委托.

MVP

在 MVP 的情况下,标准是 Presenter 通过接口、数据绑定或在被动视图的情况下通过属性与视图进行对话.使用 Passive View,属性不使用数据绑定,而是使用视图属性 getter 和 setter 直接设置控件值.

In the MVP case the standard is the Presenter talks back to the view either through an interface, databinding, or through properties in the case of Passive view. With Passive View the properties are not using databinding, instead the view property getters and setters are used to directly set the control value.

你觉得这个主意怎么样?

What do you think about that idea?

你认为ViewModel有View的接口可以吗?

Do you think that it's okay for ViewModel have the interface of View?

如果您想添加更多,欢迎添加... :)

If you like to add more then you are welcome to add... :)

关于这篇文章的整个想法是在社区中获得对 MVVM 模式的相同理解.

The whole idea about this post is to get the same understanding of MVVM pattern in Community.

推荐答案

我喜欢你写的东西.真正让我感到困扰的一件事是,很多人似乎将他们的 VM 与他们的视图紧密耦合 - 如果您这样做,那么您可能只是在做旧的 XAML + 一切都被重击到代码隐藏事情.

I like what you have written. One of the things that really bugs me is that a lot of people seem to have their VM coupled very tightly to their View - if you are doing this then you might as well just be doing the old XAML + everything whacked into the code behind thing.

我使用的模式在 MVVM 上略有不同(但大致相同).就我个人而言,我喜欢将我的 ViewModel 作为界面提供给 View - 它使分离非常干净.这在做原型时有很多好处,视觉元素可以在视图中切入或切出,而对 ViewModel 几乎没有影响.

The pattern I use is a slight variant on the MVVM (but it is mostly the same). Personally I like to have my ViewModel given to the View as an interface - it keeps the separation very clean. This has a lot of benefit when doing prototypes, visual elements can be switched in or out of the View with little or no impact on the ViewModel.

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

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