有助于理解MVVM模式?
我正在尝试加快使用 WPF 和与 MVVM 模式密切相关的 Prism 框架。我已经准备了许多关于 MVVM 的不同描述、示例和讨论,每一个都略有不同,让我有点困惑。
我的理解如下:
MVVM 模式有 3 个部分:-
- 模型 - 保存应用程序数据/信息的类。
- 视图 - 应用程序的视觉元素。
- ViewModel - 与视觉元素相关的逻辑、状态和其他行为。它从模型中获取数据并以视图可以直接使用它的方式公开它(可能通过一些数据转换/格式化)。
我不确定的是:
- 这 3 个部分是否涵盖了应用程序的每个部分?或者应用程序中是否还有这 3 部分之外的部分?
- 是 ViewModel 还是其他负责填充模型的部分?
提前致谢
I'm trying to get up to speed with using WPF and the Prism framework which is heavily associated with the MVVM pattern. I've ready many different descriptions, examples and discussions on MVVM and each one is slightly different and has left me a little confused.
My understand is as follows:
The MVVM pattern has 3 parts to it :-
- Model - the classes that hold the application data/information.
- View - The visual elements of the application.
- ViewModel - The logic, state and other behaviour associated with the visual elements. It takes data from the model and exposes it (possibly with some data conversion/formatting) in such a way that the View can use it directly.
What I'm not sure about is:
- Do these 3 parts cover every part of the application? Or can there be parts of the application that are outside these 3 parts?
- Is it the ViewModel or some other part that is responsible for populating the Model?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
绝对不是。除非他们这样做。如果您的应用程序很简单,那么一切都可以在视图、视图模型或模型中处理。如果您的应用程序很复杂,并且最佳实践要求您将逻辑分解为自己的类型(通信逻辑、存储库逻辑等),那么就没有什么可以阻止您。 MVVM 只关心 View 内以视图为中心的逻辑、ViewModel 内的应用程序逻辑以及存储两者之间传输信息的方式。
ViewModel 的唯一任务是解释用户操作并准备模型内的逻辑结果,以便视图可以向用户显示此信息。在某些情况下,模型本身保存一些逻辑以便它可以响应用户操作是有意义的。然而,根据我的经验,这种迷你视图模型-模型设计是对缺乏经验的开发人员的设计决策的反应。一旦掌握了 MVVM 的真正窍门,除了验证逻辑之外,通常不需要(或不想)将任何代码放入模型中。
Absolutely not. Unless they do. If your application is simple, then everything can be handled in the View, ViewModel or Model(s). If your application is complex, and best practices dictate you break out logic into their own types (communication logic, repository logic, etc), then there is no stopping you. MVVM is only concerned with view-centric logic within the View, application logic within the ViewModel, and the means of storing information for transmission between the two.
The ViewModel is solely tasked with interpreting user actions and readying the results of logic within Models so that the View may display this information to the user. In some cases, it makes sense that a Model itself hold some logic so that it may respond to user actions. However, it has been my experience that this mini-ViewModel-Model design is in reaction to design decisions by inexperienced developers. Once you get the real hang of MVVM, you usually don't have to (or want to) put any code in your Models apart from validation logic.
将模型、视图模型和视图视为分别处理业务、应用程序流和表示的逻辑层。例如,ViewModel类可以将复杂或可重用的 UI 交互委托给单独的服务,该服务不对应于任何特定视图,但仍属于 ViewModel层。
是的,ViewModel 位于 UI 和 Model 之间。
Think of Model, ViewModel and View as logical layers that handle Business, application flow and presentation respectively. For example, a ViewModel class may delegate complex or reusable UI interactions to a separate service that doesn't correspond to any particular view but still belongs to ViewModel layer.
Yes, ViewModel stands between UI and Model.