MVC、MVP 和 MVVM 设计模式在编码 c# 方面有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19444431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is difference between MVC, MVP & MVVM design pattern in terms of coding c#
提问by Thomas
If we search Google using the phrase "What is difference between MVC, MVP & MVVM design pattern" then we may get few URL's which discuss the difference between MVC MVP & MVVM design pattern theoretically like:
如果我们使用短语“MVC、MVP 和 MVVM 设计模式之间有什么区别”在 Google 上搜索,那么我们可能会得到很少的 URL 讨论MVC MVP 和 MVVM 设计模式之间的区别,理论上如下:
MVP
MVP
Use in situations where binding via a datacontext is not possible. Windows Forms is a perfect example of this. In order to separate the view from the model, a presenter is needed. Since the view cannot directly bind to the presenter, information must be passed to the view via an interface (IView).
在无法通过数据上下文进行绑定的情况下使用。Windows 窗体就是一个完美的例子。为了将视图与模型分开,需要一个演示者。由于视图不能直接绑定到演示者,信息必须通过接口(IView)传递给视图。
MVVM
MVVM
Use in situations where binding via a datacontext is possible. Why? The various IView interfaces for each view are removed which means less code to maintain. Some examples where MVVM is possible include WPF and javascript projects using Knockout.
在可以通过数据上下文进行绑定的情况下使用。为什么?删除了每个视图的各种 IView 接口,这意味着需要维护的代码更少。可以使用 MVVM 的一些示例包括使用 Knockout 的 WPF 和 javascript 项目。
MVC
MVC
Use in situations where the connection between the view and the rest of the program is not always available (and you can't effectively employ MVVM or MVP). This clearly describes the situation where a web API is separated from the data sent to the client browsers. Microsoft's ASP.NET MVC is a great tool for managing such situations and provides a very clear MVC framework
在视图和程序其余部分之间的连接并不总是可用的情况下使用(并且您不能有效地使用 MVVM 或 MVP)。这清楚地描述了 Web API 与发送到客户端浏览器的数据分离的情况。Microsoft 的 ASP.NET MVC 是管理此类情况的绝佳工具,并提供了非常清晰的 MVC 框架
But I have not found a single article which discuss the difference theoretically along with sample code.
但是我还没有找到一篇文章从理论上讨论差异以及示例代码。
It would be really nice if i get a article which discuss the difference between these 3 design patterns (MVC, MVP & MVVM) along with code.
如果我能有一篇文章讨论这 3 种设计模式(MVC、MVP 和 MVVM)以及代码之间的区别,那就太好了。
I'd like to get my hands on source code of 3 similar CRUDapps which has been implemented by these three design patterns (MVC, MVP & MVVM). So that I can go through the code and understand how one should write code for these three design patter (MVC, MVP & MVVM).
我想了解 3 个类似的CRUD应用程序的源代码,这些应用程序已由这三种设计模式(MVC、MVP 和 MVVM)实现。这样我就可以通过代码了解应该如何为这三种设计模式(MVC、MVP 和 MVVM)编写代码。
So if any such article exists which discuss how code would look different for these 3 design patterns (MVC, MVP & MVVM) then please redirect me to that article.
因此,如果有任何此类文章讨论了这 3 种设计模式(MVC、MVP 和 MVVM)的代码看起来有何不同,那么请将我重定向到该文章。
回答by Pritam Banerjee
Some basic differences can be written in short:
一些基本的差异可以简而言之:
MVC:
MVC:
Traditional MVC is where there is a
传统的 MVC 是
- Model: Acts as the model for data
- View : Deals with the view to the user which can be the UI
- Controller: Controls the interaction between Model and View, where view calls the controller to update model. View can call multiple controllers if needed.
- 模型:充当数据的模型
- View : 处理用户的视图,可以是 UI
- Controller:控制Model和View的交互,view调用控制器更新模型。如果需要,视图可以调用多个控制器。
MVP:
MVP:
Similar to traditional MVC but Controller is replaced by Presenter. But the Presenter, unlike Controller is responsible for changing the view as well. The view usually does not call the presenter.
类似于传统的 MVC,但 Controller 被 Presenter 取代。但是Presenter 与Controller 不同,它也负责改变视图。视图通常不会调用演示者。
MVVM
MVVM
The difference here is the presence of View Model. It is kind of an implementation of Observer Design Pattern, where changes in the model are represented in the view as well, by the VM. Eg: If a slider is changed, not only the model is updated but the data which may be a text, that is displayed in the view is updated as well. So there is a two-way data binding.
这里的区别在于视图模型的存在。它是观察者设计模式的一种实现,其中模型中的更改也由 VM 表示在视图中。例如:如果滑块发生变化,不仅模型会更新,而且视图中显示的可能是文本的数据也会更新。所以有一个双向数据绑定。
回答by taha027
Great Explanation from the link : http://geekswithblogs.net/dlussier/archive/2009/11/21/136454.aspx
链接的很好解释:http: //geekswithblogs.net/dlussier/archive/2009/11/21/136454.aspx
Let's First look at MVC
我们先来看看MVC
The input is directed at the Controller first, not the view. That input might be coming from a user interacting with a page, but it could also be from simply entering a specific url into a browser. In either case, its a Controller that is interfaced with to kick off some functionality.
输入首先指向控制器,而不是视图。该输入可能来自与页面交互的用户,但也可能来自简单地将特定 url 输入浏览器。在任何一种情况下,它都是一个控制器,用于启动某些功能。
There is a many-to-one relationship between the Controller and the View. That's because a single controller may select different views to be rendered based on the operation being executed.
控制器和视图之间存在多对一的关系。这是因为单个控制器可能会根据正在执行的操作选择要呈现的不同视图。
There is one way arrow from Controller to View. This is because the View doesn't have any knowledge of or reference to the controller.
有一种从控制器到视图的箭头。这是因为视图不了解或引用控制器。
The Controller does pass back the Model, so there is knowledge between the View and the expected Model being passed into it, but not the Controller serving it up.
控制器确实将模型传回,因此视图和传递给它的预期模型之间存在知识,而不是为它提供服务的控制器。
MVP – Model View Presenter
MVP – 模型视图展示器
Now let's look at the MVP pattern. It looks very similar to MVC, except for some key distinctions:
现在让我们看看 MVP 模式。除了一些关键区别外,它看起来与 MVC 非常相似:
The input begins with the View, not the Presenter.
输入从视图开始,而不是演示者。
There is a one-to-one mapping between the View and the associated Presenter.
View 和关联的 Presenter 之间存在一对一的映射关系。
The View holds a reference to the Presenter. The Presenter is also reacting to events being triggered from the View, so its aware of the View its associated with.
View 持有对 Presenter 的引用。Presenter 还对从 View 触发的事件做出反应,因此它知道与其关联的 View。
The Presenter updates the View based on the requested actions it performs on the Model, but the View is not Model aware.
Presenter 根据它在模型上执行的请求操作更新视图,但视图不知道模型。
MVVM – Model View View Model
MVVM - 模型视图视图模型
So with the MVC and MVP patterns in front of us, let's look at the MVVM pattern and see what differences it holds:
因此,有了摆在我们面前的 MVC 和 MVP 模式,让我们来看看 MVVM 模式,看看它有什么区别:
The input begins with the View, not the View Model.
输入从视图开始,而不是视图模型。
While the View holds a reference to the View Model, the View Model has no information about the View. This is why its possible to have a one-to-many mapping between various Views and one View Model…even across technologies. For example, a WPF View and a Silverlight View could share the same View Model.
虽然 View 持有对 View Model 的引用,但 View Model 没有关于 View 的信息。这就是为什么可以在各种视图和一个视图模型之间进行一对多映射……甚至跨技术。例如,WPF 视图和 Silverlight 视图可以共享相同的视图模型。
回答by Uddhav Gautam
MVC, MVP, MVVM
MVC、MVP、MVVM
MVC (old one)
MVC(旧的)
MVP (more modular because of its low-coupling. Presenter is a mediator between View and Model)
MVP(因为低耦合而更加模块化。Presenter是View和Model之间的中介)
MVVM (You already have two-way binding between VM and UI component, so it is more automated than MVP)