将Form.ShowDialog()代码重构为MVP [英] Refactoring Form.ShowDialog() code to MVP

查看:109
本文介绍了将Form.ShowDialog()代码重构为MVP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForm和几个属性设置在其上。

例如:名称,地址在窗体上被接受。

(在实际例子中有更多的属性)

I have a WinForm and few properties that are set on it.
for example : Name,Address are accepted on the Form.
(many more properties in actual example)

目前的实现有点类似于

frmName frmView = new  frmName (); //frmName  is WINFORM 
frmView.Name= "ABC"; //any valid string or read this from file
frmView.Address="SomeAddress"; //any valid address or read this from file

if (frmView.ShowDialog() == DialogResult.OK)
{
    //OK CLICK PROCESS and
    // get new values edited by user
     string name = frmView .Name;
     string address = frmView.Address;
     doProcessing(name,address);
}
else{
  //Ignore cancel click..
}

如何将其转换为基于Winform的 MVP 应用程序。

还需要重写在 ShowDialog() 之前完成的处理程序到演示者/模型

(dunno在哪里要做到这一点)?

还需要避免在表单本身上编写代码(被动视图)

how do i convert this to a MVP based Winform application.
Also need to refactor the processing done on ShowDialog() to the Presenter/Model
(dunno exactly where to do it)?
Also need to avoid writing code on the form itself.(Passive view)

感谢所有。

推荐答案

我还在尝试不同的MVP方法,但我目前正在做的就是这样:

I'm still experimenting with different MVP approaches myself, but the way I'm currently doing it is like so:

frmName frmView = new frmName();

if (frmView.ShowDialog() == DialogResult.OK) {
    presenter.RequestProcessing(frmView.Name, frmView.Address);
} else {
    //Ignore cancel click..
}

你说你想避免在表单本身上编写任何代码,但这对我来说没有意义。在被动视图中,将所有应用程序特定请求传递给控制器或演示者。

You say you want to avoid writing any code on the form itself, but this doesn't make sense to me. In Passive View, you pass on all application-specific requests to the controller or presenter.

在此示例中,视图处理与视图相关的逻辑。打开对话框不是一个用户的动作,任何其他的(如演示者)需要被通知。就像打开上下文菜单一样,一个对话框是这个特定视图选择向用户提供特定应用程序的请求的一部分。直到用户实际使用它并提交请求,演示者不需要知道任何内容。

In this example, the view handles view-related logic. Opening the dialog box isn't a user action that anything else (such as the presenter) needs to be informed about. Just like opening a context menu, a dialog box is part of how this particular view chooses to offer those application-specific requests to the user. Until the user actually goes through with it and submits the request, the presenter doesn't need to know anything.

在某些情况下,我需要能够处理对话框本身的错误,我已将 IPresenter 对象传递到对话框的构造函数中。例如,当点击确定按钮时,它可以使适当的演示者自己进行请求,并且可以显示一个消息框而不是在发生错误时关闭。

In some circumstances where I've needed to be able to handle errors within the dialog box itself, I've passed the IPresenter object into the dialog box's constructor. It can then make the appropriate presenter request itself when the "OK" button is clicked, for example, and can show a message box instead of closing in case of an error.

MVP有很多变化,但我希望这有助于。祝好运,设置好。

There are a lot of variations on MVP, but I hope this helps. Good luck with setting it up.

这篇关于将Form.ShowDialog()代码重构为MVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆