模型和视图模型中应该包含哪些内容?

发布于 2024-12-02 10:23:48 字数 537 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

墟烟 2024-12-09 10:23:48

MVVM 中的 ViewModel 是 中的 ApplicationModel 和 Controller 的组合经典的MVC。因此,它负责连接到服务以查询数据以显示并调用针对服务的操作。

工作流程应该是这样的:

  1. LoginViewModel 创建并获取 WCF 服务的句柄(理想情况下,您应该使用 DI 将 WCF 服务作为 ILoginService 注入)
  2. 用户输入绑定到 VM 上相应属性的用户名和密码,然后单击绑定到虚拟机上委托命令的登录。
  3. 在命令处理程序中,VM 运行验证(例如,确保已设置用户名和密码)。VM
  4. 在 ILoginService 上调用 LoginUser,传递用户名和密码(调用 WCF 服务上的操作)。

该模型不应该知道服务从技术上讲,您在客户端使用纯 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:

  1. LoginViewModel is created and gets a handle to the WCF service (ideally, you should use DI to inject the WCF service as an ILoginService)
  2. User enters username and password which are bound to corresponding properties on the VM and clicks login which is bound to a delegate command on the VM.
  3. Within the command handler the VM runs validation (e.g. make sure that username and password have been set)
  4. The VM invokes LoginUser on the ILoginService passing the username and password (invoking the operation on the WCF service.

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.

比忠 2024-12-09 10:23:48

如果您查看 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

谁许谁一生繁华 2024-12-09 10:23:48

模型不应包含数据访问或验证数据之外的任何业务逻辑。它们是数据对象,应该只包含数据和验证。

您的 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 the View.

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 the ViewModel

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文