jeudi 25 mars 2021

C# Winforms MVP: How to bind complex properties in Supervising Presenter/Controller variant?

Despite reading a lot of Q&A and blogs about this topic, I am still having trouble finding a concrete example of both Passive View and Supervising Controller comparison in code, in particular how databinding works in Winforms for "complex" objects/properties. Passive View is clear to me, but I do not understand how this is supposed to be implemented in Supervising Controller (or even if in this case, Passive View is the only alternative).

Tried to come up with a very simple example.
This can be considered as pseudo-code:

public class Model
{
    public string Name { get; set; }
    public Dictionary <string, string> PhoneNumbers { get; set; }
}

public class View : IView // Form
{
    public TextBox txtName { get; set; }

    // Supervising presenter
    // How to bind these to Model's PhoneNumbers dictionary property?
    // Should this be 1-way binding (data to view), 2-way binding, or be performed by presenter?
    // Should there be a "ViewModel" that exposes these separately somehow?
    public DropDownList cmbPhoneState { get; set; } // keys
    public TextBox txtPhoneNumber     { get; set; } // values
}

public class Presenter
{
    private IView view;
    private Model model;

    public Presenter(IView view)
    {
        this.view = view;
        this.model = new Model();
    }

    // Passive view
    // 1. Populate cmbPhoneState with keys from model's dictionary, and (for ex.) set first one selected
    // 2. Show first phone number value in textbox; when user changes selection, update view accordingly

    // Supervising presenter
    // -> ?
}

In this post it seems that the author comes up with a solution, but this seems extremely overkill to the average developer I am for such a simple requirement. I don't need 100% test coverage and I am not trying to avoid doing work; simple does not mean lazy.

How should the view bind to complex properties (ex. a dictionary, or another custom Type) in Supervising Controller pattern? Say user adds another phone number to the collection: this happens in the view, but then who notifies who, and how?
Alternatively: should this pattern be avoided in this case and more complex ones?

(please no link to Martin Fowler articles or Wikipedia in the answer; pseudo-code is very welcome)

Aucun commentaire:

Enregistrer un commentaire