Refactoring Form.ShowDialog() code to MVP

烂漫一生 提交于 2019-12-03 00:36:58

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.

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!