1
votes

I'm currently preparing core framework for our upcoming project based on WinForms and MVP design pattern.

I'm not sure, what would be the best way, how to communicate between two Views/Presenters. To be more specific - I have a ListView and a DetailView. When user clicks on an item in the ListView, I need to display edit form for this item, which in my case is DetailView.

Options:

  1. Should ListPresenter create DetailPresenter on the click event? (A Factory could be of a help.)

  2. Should instance of DetailPresenter be injected in ListPresenter constructor?

I feel 2) might be the "right" solution, but I would prefer creating DetailView/DetailPresenter just in time I really need it - i.e. when user clicks the button.

The next problem, I don't know how to go about it is the objects lifetime. When I inject a View into Presenter, who is responsible for the cleanup? I'm used to behaviour, where cleanup is made by the same party who created it. But in this case I could imagine View could be disposed by the Presenter.

I hope the questions are not too generic, I have read a lot of articles about MVC/MVP, but they mostly don't go further than showing how to implement single View-Presenter communication.

Thank you.

1
winforms is not recommended for any new projects. You should seriously consider using current, relevant technology instead.Federico Berasategui
I know WinForms haven't been updated for a while and probably never will be, but for some projects it is, in my opinion, still perfectly capable technology. Anyway, what other choice is there for a desktop .NET application? WPF? Just because it has very good data binding capabilities, it doesn't make it perfect solution for all projects. We have considered using it, but decided to still use WinForms.Tom Shane
perfectly capable technology - until you need something that does not look like Windows 3.1, or until you need to properly separate the UI from the application's behavior. what other choice is there for a desktop .NET application? - WPF is the preferred technology for .Net Windows Desktop UIs. Whatever you can achieve with winforms, you can also achieve the same in WPF with 10% the amount of code. Not to mention the customizability and ease of development to create great UX.Federico Berasategui
Well, I'm not going to agree about WinForms vs. WPF. I have used both and for some projects it is WPF for others I rather use WinForms. However, the question is valid even for MVVM. You click a button your ViewModel receives the command and now what? Does it create DetailViewModel by itself or should it expect DetailViewModel to be injected in ListViewModel constructor? Also, who is responsible for objects lifecycle? The same problem, doesn't matter if that's WinForms or WPF.Tom Shane
in MVVM the ViewModels are responsible for their "child" or "detail" VMs life cycle, and the view simply responds to that via DataBinding without needing additional piping.Federico Berasategui

1 Answers

-1
votes

You could wrap the ListPresenter and the DetailPresenter in a ListDetailPresenter.

public class ListDetailPresenter
{
    private ListPresenter _listPresenter;
    private DetailPresenter _detailPresenter;

    public ListDetailPresenter()
    {
        _listPresenter = new ListPresenter();
        _detailPresenter = new DetailPresenter();

        _listPresenter.SelectionChanged += OnSelectionChanged;
    }

    private void OnSelectionChanged(object sender, EventArgs e)
    {
        _detailPresenter.SetItem(_listPresenter.SelectedItem);
    }
}