ASP.Net MVC 和 MVVM
MVVM 是一种 Microsoft 设计模式,早于 ASP.Net MVC 就已存在。任何人都可以了解 MVVM 和新的 MVC 模式之间的差异吗?
MVVM is a Microsoft design pattern that existed before ASP.Net MVC. Can anyone through light on differences between MVVM and the new MVC pattern?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是:当使用 ASP.NET MVC 时,MVC 模式使用控制器将模型直接呈现到视图中。对于对象数量较少的琐碎项目来说,这是完全可以接受的。这可能会成为一个问题,因为 UI 层的关注点可能会渗透到底层(域)模型。
使用 MVVM 时,您将在模型和视图之间添加抽象,这当然是 ViewModel。这允许作者将最容易被视图消耗的对象投影到视图中。 ViewModel 可以包含在(域)模型中不合适的内容。这里相关的成本是您需要具有将数据从模型转置到视图模型的映射逻辑。像 AutoMapper 这样的工具可以帮助完成这项工作。
一个简单的例子可能是模型不需要某些必需的字段,但特定的视图需要。如果将此逻辑附加到 ViewModel,则其他 UI 可以使用相同的 VM,而不必重复烘焙到第一个用户界面中的逻辑,而不是将其烘焙到用户界面中。
Yes: When using ASP.NET MVC the MVC pattern uses the controller to render the model directly into the view. This is perfectly acceptable for trivial projects with a small number of objects. Where this can become a problem is that the concerns of the UI layer can bleed through to the underlying (domain) model.
When using MVVM then you are adding an abstraction between the Model and the View, which is of course the ViewModel. This allows the author to project into the view an object that is most readily consumed by the view. The ViewModel can contain things which would be out of place in the (domain) Model. The cost associated here is that you need to have mapping logic which transposes the data from the model to the View Model. Tools like AutoMapper can assist with this chore.
A simple example of this might be the Model doesn't require certain fields as required, but a particular View does. Rather than baking this logic into the user interface, if it is attached to the ViewModel, then other UI's can consume the same VM without having to duplicate logic that was baked into the first user interface.
MVC 和 MVVM 实际上是有很大不同的。当谈论 ASP MVC 时,似乎对 MVVM 存在一些误解。在 MVC 中创建“视图模型”的做法是提供视图的特定类,而好的做法并不符合 MVVM 的精神,实际上只是 MVC 的更简洁版本。
MVVM 更适合使用 WPF 或类似工具的桌面,或者纯粹使用 JavaScript 框架(例如 knockout.js)的浏览器。该模式与 MVC 完全不同,并且涉及“订阅”模型的视图。
MVC and MVVM are actually quite different. There seems to be a fair bit of misunderstanding of MVVM when talked about with ASP MVC. The practice of making 'View Models' in MVC, which are specific classes to feed views, while good practice is not true to the spirit of MVVM, and in fact is just a cleaner version of MVC.
MVVM is more suited to the desktop using WPF or similar, or purely in the browser using a JavaScript framework such as knockout.js. The pattern is quite different to MVC and involves views being 'subscribed' to the model.
我冒昧地说,MVVM 是 Microsoft 的设计模式,而 ASP.NET MVC(这是最近才出现的)是 Microsoft 的特定实现(不一定遵循 MVC 或 MVVM,但很相似)。正如 Reed 所建议的,MVC 自 70 年代以来就已存在。
I would venture to suggest that MVVM is Microsoft's design pattern and ASP.NET MVC, which is farily recent, is a specific implementation by Microsoft (that doesn't necessarily adhere to either MVC or MVVM but is similar). And as suggested by Reed, MVC has been around since the 70's.
MVC 和 MVC MVVM 是架构模式。 MVC 的根源可以追溯到 Smalltalk。 ASP.NET MVC 是 Microsoft 使用 ASP.NET 框架实现的 MVC 模式。
这两种模式都涉及关注点分离。 MVC 更多地涉及应用程序中各种常用层的交互,例如模型(数据层)、视图(表示层)和控制器(业务逻辑层)。
凭借 WPF 和 Silverlight 的高级数据绑定功能,MVVM 更适合并被宣传为下一个重大事件。 Martin Fowler 在他的《企业应用程序架构》一书中将这些模式概括为表示模式。
我认为使用 ViewModel 的一个优点是它允许您使用单元测试更好地测试应用程序代码。由于这个原因,我发现 MVVM 或至少它的 ViewModel 部分在 ASP.NET MVC 应用程序中也经常使用。
Both MVC & MVVM are architectural patterns. MVC has its roots way back to Smalltalk. ASP.NET MVC is Microsoft's implementation of the MVC pattern using ASP.NET framework.
Both the patterns deal with separation of concerns. MVC is more to do with the interaction of various commonly used layers in an application like Model (data layer), View (presentation layer) and Controller (business logic layer).
With advanced databinding capabilities of WPF and Silverlight, MVVM was more suited and publicised as the next big thing. Martin Fowler generalized these patterns as presentation patterns in his Enterprise Application Architecture book.
One advantage I see in using a ViewModel is that it allows you to test the application code better using unit tests. Because of this reason I find MVVM or at least the ViewModel bit of it being used quite often in ASP.NET MVC applications as well.