ASP.NET MVC 模型与 ViewModel
好的,我一直在听到关于 MS 的 ASP.NET MVC 的“ViewModels”的讨论。
现在,这是一种特定类型的模型,对吗?不是特定类型的视图。
据我理解,它是一种具有与视图交互的特定目的的模型?或者类似的东西?
如果有一些澄清,我们将不胜感激。
OK, I have been hearing discussion about "ViewModels" in regards to MS's ASP.NET MVC.
Now, that is intended to be a specific kind of Model, correct? Not a specific kind of View.
To my understanding, it's a kind of Model that has a specific purpose of interacting with the View? Or something like that?
Some clarification would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
本质上,模型和视图模型都是具有属性的简单类。
这些类的主要目标是为各自的受众(分别是控制器和视图)描述(“模型”)对象。
所以你说的完全正确
因此,虽然模型类实际上是与应用程序交互的域实体,但视图模型是与视图交互的简单类。
希望它有所帮助:)
更新:
Microsoft 为 Martin fowler 开发了一个专门版本的表示模式,主要基于模型-视图-控制器,并将其称为用于 PF 应用程序的模型-视图-视图模型 (MVVM) 。此模式针对现代 UI 开发平台,其中 UI 开发人员比传统开发人员有更多基于业务逻辑的不同需求。请查看此处了解一些理论
Essentially Model and View Model are both simple classes with attributes.
The main objective of these classes are to describe (to "Model") an object for their respective audiences that are respectively the controller and the view.
So you are completely right when you say
So, while Model classes are effectively Domain Entities that your application interact with, View Models are simple classes that your views interact with.
Hope it helps :)
Update:
Microsoft has developed a specialized version of the Presentation Pattern by Martin fowler largely based on the Model-View-Controller and called it Model-View-ViewModel (MVVM) for PF application. This pattern is targeted at modern UI development platforms where UI developers have different requirements based more on business logic than traditional developers. Have a look here for a bit of theory
用最简单的术语来说,我喜欢考虑以下内容:
模型:看起来和感觉完全像您的数据模型。出于所有意图和目的,它只是数据模型的类表示。它不了解您的视图或视图中的任何元素。也就是说,它不应该包含您将用于视图的任何属性装饰器(即必需的、长度等)。
视图模型:充当视图和模型之间的数据绑定器,在许多情况下,也是模型的包装器。如果没有视图,它就会变得毫无用处,因此它通常不能像标准模型那样在多个视图和控制器之间重用。
例如,您的模型可能具有以下属性,这些属性是数据源的直接表示:
现在,由于您的视图模型与视图相关联,因此它可能具有以下属性 - 它将模型的 FirstName 字段和 LastName 字段连接在一起作为一个字符串:
In the simplest of terms, I like to think of the following:
Model: Strictly looks and feels like your data model. For all intents and purposes it is only a class representation of your data model. It has no knowledge of your View or any elements within your View. That said, it should not contain any attribute decorators (ie; Required, Length, etc.) that you would use for your View.
View Model: Serves as a data-binder between your View and your Model and in many cases, is also a wrapper for your Model. It would be rendered useless without the View, so it typically isn't reusable across multiple Views and Controllers like a standard Model is.
As an example, your Model may have the following properties, which are direct representations of your data source:
Now, since your View Model is tied to your View, it may have the following property - which concatenates the Model's FirstName field and LastName field together as one string:
我发现这篇文章对于理解“域模型”和“视图模型”如何在 MVC 应用程序中交互(特别是在绑定方面)非常有用。最重要的是包括示例而不是抽象描述。
“自从 MVC 发布以来,我发现关于如何最好地构建视图模型存在很多困惑。有时这种困惑并非没有充分的理由,因为似乎没有大量关于最佳实践建议的信息。此外,没有一个“一刀切”的解决方案可以作为灵丹妙药,在这篇文章中,我将描述一些已经出现的主要模式以及每种模式的优缺点,这一点很重要。这些模式是人们在解决现实世界问题时出现的。”
http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-视图模型模式.aspx
I found this article a very useful resource for understanding how the "Domain Model" and "View Model" interact within an MVC application, particularly in regards to binding. Best of all includes examples instead of abstract descriptions.
"Since MVC has been released I have observed much confusion about how best to construct view models. Sometimes this confusion is not without good reason since there does not seem to be a ton of information out there on best practice recommendations. Additionally, there is not a “one size fits all” solution that acts as the silver bullet. In this post, I’ll describe a few of the main patterns that have emerged and the pros/cons of each. It is important to note that many of these patterns have emerged from people solving real-world issues."
http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx
维基百科对模型与模型视图的描述比您在 SO 答案中得到的更完整的描述: http:// en.wikipedia.org/wiki/Model_View_ViewModel
我引用:
模型:就像在经典的 MVC 模式中一样,模型指的是 (a) 代表真实状态内容的对象模型(面向对象的方法),或 (b) 表示该内容的数据访问层(以数据为中心的方法)。
视图:与经典的 MVC 模式一样,视图是指 GUI 显示的所有元素,例如按钮、窗口、图形和其他控件。
ViewModel:ViewModel 是“视图的模型”,这意味着它是视图的抽象,也用于视图和模型之间的数据绑定。它可以被视为控制器(在 MVC 模式中)的一个专门方面,充当数据绑定器/转换器,将模型信息更改为视图信息并将命令从视图传递到模型中。 ViewModel 公开公共属性、命令和抽象。 ViewModel 被比作数据的概念状态,而不是模型中数据的真实状态。
WikiPedia has a more complete description of Model vs. ModelView than you'll get in an SO answer: http://en.wikipedia.org/wiki/Model_View_ViewModel
I quote:
Model: as in the classic MVC pattern, the model refers to either (a) an object model that represents the real state content (an object-oriented approach), or (b) the data access layer that represents that content (a data-centric approach).
View: as in the classic MVC pattern, the view refers to all elements displayed by the GUI such as buttons, windows, graphics, and other controls.
ViewModel: the 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. It could be seen as a specialized aspect of what would be a Controller (in the MVC pattern) that acts as a data binder/converter that changes Model information into View information and passes commands from the View into the Model. The ViewModel exposes public properties, commands, and abstractions. The ViewModel has been likened to a conceptual state of the data as opposed to the real state of the data in the Model.
有一个 ViewModel 的概念,但它通常与 Asp.net MVC 无关。 MVC 使用模型视图控制器模式,其中控制器处理交互,从模型构建数据,然后将该数据传递到视图进行显示。
ViewModel(以及模型视图 ViewModel 模式)通常与 Silverlight 和 WPF 相关。 Xaml 有点不同,因为视图可以双向绑定到 ViewModel,因此技术也有点不同。例如,如果将文本框绑定到字段,则当您在该文本框中键入内容时,该字段的值会动态更新。这种交互在网页中实际上是不可能的,因为网页是无状态的。
这两种模式的相似之处在于它们都试图将逻辑与显示分开。最常见的用途/原因是测试:您希望能够从代码(通过测试框架)执行用户将通过用户界面调用的所有交互。
There is a notion of a ViewModel, but it is not generally associated with Asp.net MVC. MVC uses the Model View Controller patter, where the controller handles interactions, builds up data from the Model, and then passes that data to the View for display.
ViewModels (and the Model View ViewModel pattern) is more generally associated with Silverlight and WPF. Xaml is a bit different in that the views can do two-way binding to the ViewModels, so the technology is a little different. For example, if you bind a textbox to a field, as you type into that textbox, the value of the field is updated dynamically. This sort of interaction isn't really possible in web pages since web pages are stateless.
The similarity in the two patterns is that they are both trying to separate the logic from the display. The most common use/reason for this is testing: you want to be able to perform from code (via a testing framework) all the interactions that a user will invoke via the User Interface.