Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVP Call a form

Tags:

c#

mvp

winforms

I try to implement the ModelViewPresenter(MVP) pattern in an WinForms application. I have a mainform with a button, and when I click on this button, a new form is shown. What is the best approach to follow the MVP pattern? I think, I should open this second form from my presenter, so that the view doesn't know anything about other views. But I am not sure. My Application has only three dialogs. What is the best way?

Code

        private void LoadAndShowMasterTrackForm()
    {
        if (GvClaims.SelectedRowsCount < 1)
            return;

        // Create an empty list.
        var rowsToUpdate = new List<TrackData>();
        // Add the selected rows to the list.
        for (int i = 0; i < GvClaims.SelectedRowsCount; i++)
        {
            if (GvClaims.GetSelectedRows()[i] >= 0)
            {
                var track = GvClaims.GetRow(GvClaims.GetSelectedRows()[i]) as TrackData;
                if (track != null)
                    rowsToUpdate.Add(track);
            }
        }
        using (var uow = new UnitOfWork())
        {
            try
            {
                GvClaims.BeginUpdate();
                var frmSynonyms = new MasterTrackDialog { DataSourceMainGridView = rowsToUpdate, DataSource = rowsToUpdate, SessionUow = uow };
                var result = frmSynonyms.ShowDialog(this);
                if (result == DialogResult.Cancel)
                    uow.RollbackTransaction();
            }
            catch (Exception ex)
            {
                uow.RollbackTransaction();
                Logger.Error(ex.Message, ex);
            }
            finally
            {
                GcClaims.RefreshDataSource();
                GvClaims.EndUpdate();
            }
        }

    }

Thanks!

like image 386
MrScf Avatar asked Jan 25 '26 06:01

MrScf


1 Answers

In MVP View 'owns' Presenter, not the other way around. Presenter is about the presentation logic, not about which view and how should be opened.

Therefore you cannot (should not) instantiate and open views in presenters.

Look at it this way: Presenter typically only have a reference to the View interface, so you may potentially have a console view and a winform view. Therefore Presenter is not responsible to know which type of view is currently "online" and how to spin up one.

What Presenter can do is to command a view to do something. For example, it can call view.ConfirmOperation() and it is up to the view now how to do it in the UI. The console view may prompt in a command line while a winforms view may open a dialog.

The important part here is that Presenter does not "think" in terms of dialogs etc., it "thinks" in terms of business logic (an operation needs to be confirmed, etc).

Hope it helps.

like image 109
Alexey Raga Avatar answered Jan 27 '26 20:01

Alexey Raga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!