模型和视图模型中应该包含哪些内容?
我正在开发一个 WPF 客户端,它与 WCF Web 服务交互,该服务使用 NHibernate 将数据保存在数据库上。
客户端将使用 MVVM 设计模式进行 UI 并通过发送和接收 DTO 与 WCF 服务进行交互。
据我了解,视图模型中只应包含表示数据和逻辑,而模型应包含应用程序的数据和业务逻辑。
现在让我们以客户端中的登录屏幕为例。
- 视图将代表登录屏幕本身,其数据上下文将是视图模型。
- 视图模型将保存模型的实例,并通过实现 INotifyProperty 接口来更新视图。
这是我的问题: Model 中到底应该包含什么?
它是否应该使用 MEF 等依赖注入容器来保存对 WCF 代理接口的引用?
基本上,用户将输入用户名和密码,这将由于数据绑定而更新视图模型中的属性。每当用户按下视图上的“登录”按钮时,就会向视图模型发送一条命令,视图模型又将其转发给模型。然后,该模型使用代理接口与 WCF Web 服务进行通信。
这种做法正确吗?如果不是,模型和视图模型中究竟应该包含什么?
I'm developing a WPF Client which interacts with a WCF Web Service which persists data on a database using NHibernate.
The client will use the MVVM design pattern for the UI and interacts with the WCF Service by sending and receiving DTOs.
From what I understand, only presentation data and logic should go in the View-Model, whereas the Model should contain the application's data and business logic.
Now let's use for example the case of a Login screen in the Client.
- The View will represent the Login screen itself and its data context will be the View-Model.
- The View-Model will hold an instance of the Model and will update the View by implementing the INotifyProperty interface.
Here's my question: what exactly should go in the Model ?
Should it hold a reference to an interface of a WCF proxy using a dependency injection container such as MEF ?
Basically, the user would type in a username and password, which would update the properties in the view-model due to the databinding. Whenever the user presses the "Login" button on the view, a command is sent to the View-Model which in turn forwards it to the Model. The model then uses the Proxy interface to communicate with the WCF Web Service.
Is this approach correct ? If not, what exactly should go in the Model and View-Model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MVVM 中的 ViewModel 是 中的 ApplicationModel 和 Controller 的组合经典的MVC。因此,它负责连接到服务以查询数据以显示并调用针对服务的操作。
工作流程应该是这样的:
该模型不应该知道服务从技术上讲,您在客户端使用纯 DTO,因此它应该具有最少的逻辑,在服务后面应该有一个具有业务逻辑和验证的更丰富的模型。
The ViewModel in MVVM is a combination of the ApplicationModel and Controller in classic MVC. As such it is responsible for connecting to services to query for data to display and invoke operations against the service.
The workflow should be something like this:
The model shouldn't be aware of services and the like. Technically, you are using pure DTOs on the client side so it should have minimal logic, behind the service, there should be a richer model that does have business logic and validation.
如果您查看 Karls Shiffletts 博客,您会发现关于 MVVM 层的一个很好的图表。
我想说你的实现对我来说看起来不错。
这取决于您的实现,很可能您将切换 ViewModel 并为 ViewModel 提供一个 DataTemplate(视图)。此 CodeProject 链接显示了一个很好的实现:Creating an Internationalized Wizard in WPF
If you check Karls Shiffletts blog you'll find a good diagram about the MVVM-Layers.
I would say your implementation looks okay to me.
It depends on you implementation, most propably you will switch the ViewModel and have a DataTemplate (the View) for the ViewModel. This CodeProject link shows a good implementation: Creating an Internationalized Wizard in WPF
模型
不应包含数据访问或验证数据之外的任何业务逻辑。它们是数据对象,应该只包含数据和验证。您的
ViewModels
负责运行您的应用程序,这包括填充数据对象(模型)、更改它们、保存/删除它们以及将它们(和其他相关属性)提供给查看
。如果您有兴趣,我有一个简单的 MVVM 示例这里。最顶部有一个定义,它定义了与
ViewModel
相比应该进入Model
的内容Models
should not contain data access or any business logic beyond validating their data. They are data objects, and should contain nothing more than data and validation.Your
ViewModels
are in charge of running your application, and this includes filling the data objects (Models), altering them, saving/deleting them, and providing them (and other related properties) to theView
.I have a simple MVVM example here if you're interested. There is a definition at the very top that defines what should go into the
Model
compared to theViewModel