MVC 和 MVVM 有什么区别? [英] What is the difference between MVC and MVVM?

查看:19
本文介绍了MVC 和 MVVM 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准的模型视图控制器"模式和微软的模型/视图/视图模型模式有区别吗?

Is there a difference between the standard "Model View Controller" pattern and Microsoft's Model/View/ViewModel pattern?

推荐答案

MVC/MVVM 不是非此即彼的选择.

这两种模式在 ASP.Net 和 Silverlight/WPF 开发中以不同的方式出现.

MVC/MVVM is not an either/or choice.

The two patterns crop up, in different ways, in both ASP.Net and Silverlight/WPF development.

对于 ASP.Net,MVVM 用于双向绑定视图中的数据.这通常是客户端实现(例如使用 Knockout.js).另一方面,MVC 是一种分离关注点的方式在服务器端.

For ASP.Net, MVVM is used to two-way bind data within views. This is usually a client-side implementation (e.g. using Knockout.js). MVC on the other hand is a way of separating concerns on the server-side.

对于 Silverlight 和 WPF,MVVM 模式更具包容性,并且可以出现作为 MVC(或将软件组织成不同职责的其他模式)的替代品.这种模式经常出现的一个假设是 ViewModel 简单地替换了 MVC 中的控制器(就好像你可以替换 VM对于 C 的首字母缩写词,一切都会被原谅)...

For Silverlight and WPF, the MVVM pattern is more encompassing and can appear to act as a replacement for MVC (or other patterns of organising software into separate responsibilities). One assumption, that frequently came out of this pattern, was that the ViewModel simply replaced the controller in MVC (as if you could just substitute VM for C in the acronym and all would be forgiven)...

问题是:要独立测试*,尤其是在需要时可重用,视图模型不知道显示的是什么视图,但更重要的是不知道它的数据来自哪里.

The problem is: that to be independently testable*, and especially reusable when needed, a view-model has no idea what view is displaying it, but more importantly no idea where its data is coming from.

*注意:实际上,控制器从 ViewModel 中删除了大部分需要单元测试的逻辑.然后,VM 变成了一个不需要测试的哑容器.这是一件好事,因为 VM 只是设计人员和编码人员之间的桥梁,所以应该保持简单.

*Note: in practice Controllers remove most of the logic, from the ViewModel, that requires unit testing. The VM then becomes a dumb container that requires little, if any, testing. This is a good thing as the VM is just a bridge, between the designer and the coder, so should be kept simple.

即使在 MVVM 中,控制器通常也会包含所有处理逻辑,并决定使用哪些视图模型在哪些视图中显示哪些数据.

Even in MVVM, controllers will typically contain all processing logic and decide what data to display in which views using which view models.

从我们目前所见,ViewModel 模式的主要好处是从 XAML 代码隐藏中删除代码使 XAML 编辑成为一项更加独立的任务.我们仍然会在需要时创建控制器来控制(没有双关语)我们应用程序的整体逻辑.

From what we have seen so far the main benefit of the ViewModel pattern to remove code from XAML code-behind to make XAML editing a more independent task. We still create controllers, as and when needed, to control (no pun intended) the overall logic of our applications.

  • 视图显示特定形状的数据.他们不知道数据来自哪里.
  • ViewModels 保存特定形状的数据和命令,它们不知道数据或代码来自哪里或如何显示.
  • 模型保存实际数据(各种上下文、存储或其他方法)
  • 控制器监听并发布事件.控制器提供控制可见数据和位置的逻辑.控制器向 ViewModel 提供命令代码,以便 ViewModel 实际上是可重用的.
  • Views display a certain shape of data. They have no idea where the data comes from.
  • ViewModels hold a certain shape of data and commands, they do not know where the data, or code, comes from or how it is displayed.
  • Models hold the actual data (various context, store or other methods)
  • Controllers listen for, and publish, events. Controllers provide the logic that controls what data is seen and where. Controllers provide the command code to the ViewModel so that the ViewModel is actually reusable.

我们还注意到 Sculpture 代码-gen 框架实现了 MVVM 和类似于 Prism 的模式,并且它还广泛使用控制器来分离所有用例逻辑.

We also noted that the Sculpture code-gen framework implements MVVM and a pattern similar to Prism AND it also makes extensive use of controllers to separate all use-case logic.

我已经开始写博客了我将尽可能添加的主题(仅在主机丢失时存档).将 MVCVM 与通用导航系统结合存在一些问题,因为大多数导航系统只使用视图和虚拟机,但我将在后面的文章中详细介绍.

I have started a blog on this topic which I will add to as and when I can (archive only as hosting was lost). There are issues with combining MVCVM with the common navigation systems, as most navigation systems just use Views and VMs, but I will go into that in later articles.

使用 MVCVM 模型的另一个好处是在应用程序的生命周期中,只有控制器对象需要存在于内存中,并且控制器主要包含代码和很少的状态数据(即很小的内存开销).与必须保留视图模型的解决方案相比,这使得内存密集型应用程序的内存密集型应用程序少得多,并且它非常适合某些类型的移动开发(例如,使用 Silverlight/Prism/MEF 的 Windows Mobile).这当然取决于应用程序的类型,因为您可能仍需要偶尔保留缓存的 VM 以提高响应速度.

An additional benefit of using an MVCVM model is that only the controller objects need to exist in memory for the life of the application and the controllers contain mainly code and little state data (i.e. tiny memory overhead). This makes for much less memory-intensive apps than solutions where view-models have to be retained and it is ideal for certain types of mobile development (e.g. Windows Mobile using Silverlight/Prism/MEF). This does of course depend on the type of application as you may still need to retain the occasional cached VMs for responsiveness.

注意:这篇文章已经编辑了很多次,并没有专门针对所提出的狭隘问题,所以我已经更新了第一部分,现在也涵盖了.在下面的评论中,大部分讨论仅与 ASP.Net 相关,与更广泛的图景无关.这篇文章旨在介绍 MVVM 在 Silverlight、WPF 和 ASP.Net 中的更广泛使用,并试图阻止人们用 ViewModel 替换控制器.

这篇关于MVC 和 MVVM 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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