当前的 MVVM 视图模型是否违反了单一职责原则? [英] Are current MVVM view model practices a violation of the Single Responsibility Principle?

查看:44
本文介绍了当前的 MVVM 视图模型是否违反了单一职责原则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据当前的实践(至少在 WPF 和 Silverlight 中),我们会看到通过视图模型中的命令绑定绑定的视图,或者我们至少会看到在视图模型中处理的视图事件.这似乎违反了SRP,因为视图模型不仅仅对视图状态进行建模,但响应视图(用户).其他人询问了如何在不违反 SRP 的情况下构建视图模型 或询问 他们的实现是否这样做(最后一个是MVC中的控制器,但大致类似).

With current practices (at least with WPF and Silverlight) we see views bound via command bindings in the view model or we at least see view events handled in view models. This appears to be a violation of SRP because the view model doesn't just model the view state, but responds to the view (user). Others have asked how to build view models without violating SRP or asked whether their implementations do so (this last is the controller in MVC, but roughly analogous).

那么当前的做法是否违反了 SRP?或者是视图模型"真的是不违反SRP的东西的集合吗?稍微概括一下,似乎我们需要知道什么是单一职责,或者概念中是否有多个职责,是否将各个职责分开,符合 SRP.我不确定.

So are current practices a violation of SRP? Or is "view model" really a collection of things that don't violate SRP? To frame this a bit, it seems we need to know what is the single responsibility or if there are multiple responsibilities in the concept, are the individual responsibilities split out, conforming to SRP. I'm not sure.

维基百科对视图模型的定义

[T]他的 ViewModel 是视图的模型",这意味着它是视图的抽象,也用于视图和模型之间的数据绑定

[T]he ViewModel is a "Model of the View" meaning it is an abstraction of the View that also serves in data binding between the View and the Model

这对于 SRP 来说似乎已经足够了,但随后条目说(我的重点已添加)

This seems good enough for SRP, but then the entry later says (my emphasis added)

[ViewModel] 充当数据绑定器/转换器,将模型信息转换为视图信息并将命令从视图传递到模型

[The ViewModel] acts as a data binder/converter that changes Model information into View information and passes commands from the View into the Model

Prism 博客文章关于视图模型的作用,作者说(再次强调)

In a Prism blog post about the view model's role, the author says (again, my emphasis)

归结起来,视图模型是以下内容的组合:

  • 视图的抽象
  • 命令
  • 价值转换器
  • 查看状态

我确定我错过了很多定义,但它们似乎属于以下类别:

I'm sure I've missed many definitions out there, but they seem to fall into these categories:

  1. 单个模糊"建模视图状态的责任(那我们该怎么办?平均按州)
  2. 多重职责(查看状态、用户交互(即命令))
  3. 单个特定职责的组合(抽象、状态、交互、转换),因此只有一个责任:管理所有这些东西".

如果你好奇,我关心"关于这一点,因为 (2) 感觉是对的,但似乎与流行的实现背道而驰.

If you're curious, I "care" about this because (2) feels right, but seems counter to the prevailing implementations.

推荐答案

Single Responsibility as Martin定义它:

Single Responsibility as Martin defines it:

改变班级的理由永远不应该超过一个."

"THERE SHOULD NEVER BE MORE THAN ONE REASON FOR A CLASS TO CHANGE."

就 MVVM 而言,ViewModel 实际上只是 Presentation Model.

A ViewModel, as far as MVVM is concerned is really just a specialized implementation of a Presentation Model.

因此,虽然有人认为 Presentation Model 应该只代表 UI 的状态,但 Presenter/Controller 应该始终代理 UI 和演示模型之间的命令.如果遵循这个想法,将SRP 划分为状态和命令,那么添加一个命令应该不会影响代表状态的类.因此 MVVM 会破坏 SRP.

So while it could be argued that a Presentation Model should only represent the state of the UI, and that a Presenter/Controller should always broker the commands between the UI and the Presentation Model. If one follows this idea, with SRP dividing on State and Commands, then adding a command should not affect the class that represents state. Therefore MVVM would break SRP.

我认为这是抓住稻草.MVVM 是一个相当专业的实现,基本上用于 WPF/Silverlight(以及现在的浏览器客户端).

I think this is grasping at straws. MVVM is a fairly specialized implementation used basically in WPF/Silverlight (and now browser clients).

模式旨在使设计更简单,而替代方案会更麻烦或更难维护.由于 MVVM 旨在利用表示技术极其丰富的数据绑定功能,因此值得进行权衡.

Patterns are designed to make designs simpler where the alternative would be more cumbersome or less maintainable. Since MVVM is designed to take advantage of the extremely rich data binding capabilities of the presentation technologies, then it is a worthwhile trade off.

这篇关于当前的 MVVM 视图模型是否违反了单一职责原则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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