Refactoring Form.ShowDialog() code to MVP

前端 未结 1 1699
逝去的感伤
逝去的感伤 2021-02-04 18:42

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)

The current

相关标签:
1条回答
  • 2021-02-04 19:10

    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.

    0 讨论(0)
提交回复
热议问题