我是否正确实现了 MVP/演示模型 UI 模式?

发布于 2024-09-13 13:38:35 字数 1995 浏览 13 评论 0原文

我正在重新设计 Winforms 应用程序,并希望采用 演示模型模式的变体 对于用户界面。有人可以从以下解释中告诉我我的做法是否正确吗?


我决定按如下方式设置依赖关系:

   Model <---- Presentation Model <---- View

即:

  • 模型除了自身之外不知道任何东西。

  • 表示模型具有对模型的引用(但反之则不然)。

  • 视图具有对表示模型的引用(但反之则不然)。

我使用 Winforms 数据绑定来保持视图和演示模型同步。

现在,这一切都像一个魅力,除了当我需要处理例如单击表单的“关闭”按钮时。由于表示模型没有对视图的引用,因此它无法订阅视图发布的任何事件。因此,我想出了以下拐杖:

Presentation Model                   View
+--+                                 +--+
|  |                                 |  |
|  |                                 | <--------X closeButton.Click event fires
|  |                                 |  |
|  |                         +--------X |
|  |   CloseRequested = true |       |  | 
|  |                         +--------> |
|  |                                 |  |
|  | CloseRequested   CloseRequested |  |
| <-----------------------------------< |
|  |                                 |  |
| X--------+                         |  |
|  |       | IsClosed = true         |  |
| <--------+                         |  |
|  |                                 |  |
|  | IsClosed              MustClose |  |
| >-----------------------------------> |
|  |                                 |  |
|  |                                 | X--------> view.Close()
|  |                                 |  |
+--+                                 +--+

即:

  • 用户单击“关闭”按钮。

  • 按钮的 Click 事件在视图中捕获,视图通过设置属性 CloseRequested 做出反应。

  • 数据绑定将此值传输到表示模型中的相应属性。

  • 表示模型通过设置其属性 IsClosed 对此更改做出反应。

  • 数据绑定将此值传输到视图的 MustClose 中。

  • 视图通过关闭自身来响应此更改。

演示模型与视图很好地解耦,反之亦然,但是仅处理单个按钮命令就需要大量工作。考虑到我已经决定的依赖关系图,是否有更简单的方法?

I'm re-working a Winforms application and would like to employ a variation of the Presentation Model pattern for the UI. Could someone tell me from the following explanations if I'm doing it correctly?


I have decided to set up dependencies as follows:

   Model <---- Presentation Model <---- View

That is:

  • The model is not aware of anything except itself.

  • The presentation model has a reference to the model (but not vice versa).

  • The view has a reference to the presentation model (but not vice versa).

I am using Winforms data binding to keep the view and the presentation model synchronised.

Now this all works like a charm, except when I need to deal with e.g. a click on a form's "Close" button. Since the presentation model has no reference to the view, it cannot subscribe to any events published by the view. Thus I've come up with the following crutch:

Presentation Model                   View
+--+                                 +--+
|  |                                 |  |
|  |                                 | <--------X closeButton.Click event fires
|  |                                 |  |
|  |                         +--------X |
|  |   CloseRequested = true |       |  | 
|  |                         +--------> |
|  |                                 |  |
|  | CloseRequested   CloseRequested |  |
| <-----------------------------------< |
|  |                                 |  |
| X--------+                         |  |
|  |       | IsClosed = true         |  |
| <--------+                         |  |
|  |                                 |  |
|  | IsClosed              MustClose |  |
| >-----------------------------------> |
|  |                                 |  |
|  |                                 | X--------> view.Close()
|  |                                 |  |
+--+                                 +--+

That is:

  • The user clicks the "Close" button.

  • The button's Click event is captured in the view, which reacts by setting the property CloseRequested.

  • Data binding transfers this value to a corresponding property in the presentation model.

  • The presentation model reacts to this change by setting its property IsClosed.

  • Data binding transfers this value into the view's MustClose.

  • The view reacts to this change by closing itself.

The presentation model is quite nicely decoupled from the view, and vice versa, however this is a lot of work only to process a single button command. Is there an easier way, given the dependency graph I've decided on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

恋你朝朝暮暮 2024-09-20 13:38:35

我最近将 Windows 窗体应用程序转换为 MVP 架构,看起来您已经以与我所做的类似的方式设置了依赖项。但是,我只有一个 IPresenter 接口,它定义了允许视图传递用户请求的方法。由于视图已经依赖于演示者和对其的引用,因此直接调用其请求方法似乎是明智的。

因此,在我的系统中,演示者监听模型中的事件,并触发自己的演示事件,以供任何感兴趣的视图监听。视图通过适当更新自身来响应这些事件,并在发出用户请求时将其转发给呈现器。

I've recently been converting a Windows Forms application to the MVP architecture, and it looks like you've set up your dependencies in a similar way to what I've been doing. However, I simply have an IPresenter interface which defines methods to allow the view to pass on user requests. As the view already has a dependency on the presenter and a reference to it, it seems sensible to simply call request methods on it directly.

So in my system, the presenter listens to events from the model and fires its own presentation events for any interested view to listen out for. The view responds to those events by updating itself as appropriate, and forwards user requests to the presenter when they are made.

浪漫之都 2024-09-20 13:38:35

这只是我的意见。

使用表示模型需要 100% UI 支持数据绑定。即使 WPF 也不会将 CLOSE 操作设为可绑定。很多事情在演示模型中无法顺利工作,例如 MessageBox 确认。即使可以用Presenter接口抽象出来,但味道还是不好,牺牲了简单性。

另一方面,表示模型的主要目的是测试视图逻辑。在某些情况下,如果您的“关闭操作”应该进行单元测试,因为在关闭之前有一些逻辑,那么您的代码是唯一的选择。但如果这只是一个简单的确认“您确定要退出吗”,那么您最好将其放在视图上而不是呈现模型中,因为不需要进行单元测试。

it is only my opinion.

Working with presentation model require 100% UI support for data binding. Even WPF don't make the CLOSE action as Bindable. A lot of thing will not working smoothly in presentation model such as MessageBox Confirmation. even it can be abstracted with Presenter interface but still doesn't taste good and simplicity is sacrificed.

in the other hand, the main purposes of presentation model is to Test The View Logic. In some cases if your "Close Action" should be unit tested because of there is some logic before it is close then your code is the only choice. but if it is only a simple Confirmation "Are you sure you want to quit" then you better put that on the View not in presentation model because it is not require to unit test.

最美不过初阳 2024-09-20 13:38:35

该视图引用了
演示模型(但不是副
反之亦然)。

据我所知,演示文稿应该有一个更准确的视图参考接口 IView,以便演示文稿不会与具体视图耦合。然后在演示类中,您可以调用视图方法并通过 IView 订阅视图事件。

The view has a reference to the
presentation model (but not vice
versa).

AFAIK the presentation should have a refference to view more exactly to interface IView so that the presentation dosen't couple with concrete view. Then in presentation class you may call view methods and subscribe to view events via IView.

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