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

本文是小编为大家收集整理的关于MVC和MVVM的区别是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

标准"模型视图控制器"模式与Microsoft的Model/View/ViewModel模式之间有区别吗?

推荐答案

mvc/mvvm不是/或选择.

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

对于ASP.NET,MVVM用于视图中的双向绑定数据.这通常是客户端的实现(例如使用quotigout.js).另一方面,MVC是在服务器端.

上分开关注的一种方法.

对于Silverlight和WPF,MVVM模式更具包容性,可以出现作为替代MVC(或将软件的其他模式组织成单独的职责).一个假设通常是从这种模式中出来的,是ViewModel只是在MVC中替换了控制器(好像您可以在首字母缩写词中替换VM代替C,所有这些都将被原谅). .

ViewModel确实不是一定要替换单独的控制器的需求.

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

*注意:在实践中,控制器从ViewModel中删除了需要单元测试的大部分逻辑.然后,VM成为一个愚蠢的容器,几乎不需要(如果有)进行测试.这是一件好事,因为VM只是设计师和编码器之间的桥梁,因此应该保持简单.

即使在MVVM中,控制器通常都包含所有处理逻辑,并确定要显示哪些视图使用哪些视图模型的视图.

从我们到目前为止所看到的视图模式的主要好处是从XAML代码 - 主题中删除代码,以使XAML编辑更独立的任务.我们仍然在需要时创建控制器,以控制应用程序的总体逻辑(无双关).

我们遵循的基本MVCVM指南是:

  • 视图显示一定的数据形状.他们不知道数据来自何处.
  • viewModels 保留某种数据形状的数据和命令,他们不知道数据或代码来自何处或如何显示.
  • 模型保存实际数据(各种上下文,存储或其他方法)
  • 控制器聆听并发布事件.控制器提供控制数据和何处的逻辑.控制器向ViewModel提供命令代码,使ViewModel实际上可重复使用.

我们还指出, sculpture code- sculpture code- Gen Framework 实现MVVM和类似于Prism的模式,并且还广泛使用控制器将所有用例逻辑分开.

不要假设控制器被视图模型过时.

我已经在这个博客上开设了一个博客我会在可能的时候添加到的话题(仅在托管丢失时存档).将MVCVM与通用导航系统相结合的问题,因为大多数导航系统仅使用视图和VM,但是我将在以后的文章中进行.

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

注意:这篇文章已经进行了多次编辑,并且没有专门针对狭窄的问题,因此我也更新了第一部分,现在也涵盖了该问题.在下面的评论中,大部分讨论仅与ASP.NET有关,而不涉及更广泛的情况.这篇文章旨在涵盖Silverlight,WPF和ASP.NET中MVVM的广泛使用,并试图阻止人们用ViewModels代替控制器.

其他推荐答案

我认为了解这些首字母缩写词的最简单方法是暂时忘记它们.取而代之的是,考虑一下他们源于它们的软件.它确实归结为早期网络和桌面之间的区别.

随着2000年代中期的复杂性,MVC软件设计模式(最初是在1970年代描述)开始应用于Web应用程序.思考数据库,HTML页面和介于两者之间的代码.让我们简单地完善它以到达MVC:for»database«,让我们假设数据库加上接口代码.对于»html页面«,让我们假设HTML模板加模板处理代码.对于»中间«的代码,让我们假设代码映射用户单击操作,可能会影响数据库,绝对导致显示另一个视图.就是这样,至少出于此比较的目的.

让我们保留此网络内容的一个功能,不是今天,而是十年前的,当JavaScript是一个低矮的,卑鄙的烦恼,真正的程序员对此很好地避免了:HTML页面本质上是HTML页面愚蠢而被动.浏览器是瘦客户端,或者如果您愿意的话,是一个糟糕的客户.浏览器中没有智能.整页重新加载规则. »视图«每次都会重新生成.

让我们记住,与台式机相比,这种网络方式虽然风靡一时,但却落后了.如果您愿意,桌面应用程序是胖客户或富裕客户. (即使是像Microsoft Word这样的程序,也可以将其视为某种客户端,是文档的客户.)他们是充满智能的客户,充满了有关其数据的知识.他们是陈述的.他们缓存他们在内存中处理的数据.没有像整页重新加载这样的废话.

这种丰富的桌面方式可能是第二个首字母缩写的起源MVVM.不要因为省略C.控制器的遗漏而被字母愚弄.他们需要成为.没有什么可以删除.我们只是添加一件事:状态,数据缓存在客户端上(以及IT智能以处理该数据).该数据基本上是客户端上的缓存,现在被称为»viewModel«.这就是允许丰富互动的原因.就是这样.

  • mvc =模型,控制器,视图=基本上是单向通信=差的交互性
  • mvvm =模型,控制器,缓存,视图=双向通信=富互动

我们可以看到,使用Flash,Silverlight和 - 最重要的是 - JavaScript,Web已接管MVVM.浏览器不再被合法地称为瘦客户.查看他们的可编程性.查看他们的记忆消耗.查看现代网页​​上的所有JavaScript互动性.

就个人而言,我发现通过查看具体现实中的内容,我发现这个理论和首字母缩写业务更容易理解.抽象概念很有用,尤其是在混凝土物质上证明时,因此理解可能会完整圈子.

其他推荐答案

mvvm model-view viewModel

控制器被A ViewModel 取代. ViewModel位于UI层下方. ViewModel公开了视图所需的数据和命令对象.您可以将其视为视图将其数据和操作从中获取的容器对象. ViewModel从模型中获取数据.

Russel East 博客会详细讨论为什么MVVM与MVC不同

本文地址:https://www.itbaoku.cn/post/627474.html

问题描述

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

推荐答案

MVC/MVVM is not an either/or choice.

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

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.

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 ViewModel does not necessarily replace the need for separate Controllers.

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.

*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.

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

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.

The basic MVCVM guidelines we follow are:

  • 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.

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.

Don't assume controllers are made obsolete by View-models.

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.

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.

Note: This post has been edited numerous times, and did not specifically target the narrow question asked, so I have updated the first part to now cover that too. Much of the discussion, in comments below, relates only to ASP.Net and not the broader picture. This post was intended to cover the broader use of MVVM in Silverlight, WPF and ASP.Net and try to discourage people from replacing controllers with ViewModels.

其他推荐答案

I think the easiest way to understand what these acronyms are supposed to mean is to forget about them for a moment. Instead, think about the software they originated with, each one of them. It really boils down to just the difference between the early web and the desktop.

As they grew in complexity in the mid-2000s, the MVC software design pattern - which was first described in the 1970s - began to be applied to web applications. Think database, HTML pages, and code inbetween. Let's refine this just a little bit to arrive at MVC: For »database«, let's assume database plus interface code. For »HTML pages«, let's assume HTML templates plus template processing code. For »code inbetween«, let's assume code mapping user clicks to actions, possibly affecting the database, definitely causing another view to be displayed. That's it, at least for the purpose of this comparison.

Let's retain one feature of this web stuff, not as it is today, but as it existed ten years ago, when JavaScript was a lowly, despicable annoyance, which real programmers did well to steer clear of: The HTML page is essentially dumb and passive. The browser is a thin client, or if you will, a poor client. There is no intelligence in the browser. Full page reloads rule. The »view« is generated anew each time around.

Let's remember that this web way, despite being all the rage, was horribly backward compared to the desktop. Desktop apps are fat clients, or rich clients, if you will. (Even a program like Microsoft Word can be thought of as some kind of client, a client for documents.) They're clients full of intelligence, full of knowledge about their data. They're stateful. They cache data they're handling in memory. No such crap as a full page reload.

And this rich desktop way is probably where the second acronym originated, MVVM. Don't be fooled by the letters, by the omission of the C. Controllers are still there. They need to be. Nothing gets removed. We just add one thing: statefulness, data cached on the client (and along with it intelligence to handle that data). That data, essentially a cache on the client, now gets called »ViewModel«. It's what allows rich interactivity. And that's it.

  • MVC = model, controller, view = essentially one-way communication = poor interactivity
  • MVVM = model, controller, cache, view = two-way communication = rich interactivity

We can see that with Flash, Silverlight, and - most importantly - JavaScript, the web has embraced MVVM. Browsers can no longer be legitimately called thin clients. Look at their programmability. Look at their memory consumption. Look at all the Javascript interactivity on modern web pages.

Personally, I find this theory and acronym business easier to understand by looking at what it's referring to in concrete reality. Abstract concepts are useful, especially when demonstrated on concrete matter, so understanding may come full circle.

 

其他推荐答案

MVVM Model-View ViewModel is similar to MVC, Model-View Controller

The controller is replaced with a ViewModel. The ViewModel sits below the UI layer. The ViewModel exposes the data and command objects that the view needs. You could think of this as a container object that view goes to get its data and actions from. The ViewModel pulls its data from the model.

Russel East does a blog discussing more in detail Why is MVVM is different from MVC