MVP in Winforms

I am mostly from ASP.Net background with some MVC. I also made a little Silverlight and MVVM, but now I'm going to switch to Winforms, I have very little experience, so I'm wondering how to deal with MVP.

Typical MVP samples show that the presenter sets the view property (through some IView interface), while the specific view places this property value in a text field, for example. Instead of this archaic approach, you can use INotifyPropertyChanged in MVP, and if so, how? A very quick example would be really helpful!

If I were to create a model that implements INotifyPropertyChanged, doesn’t this look like MVVM? (i.e., the host updates the model and through the magic of INotifyPropertyChanged the view is updated). But wherever I read about MVVM and Winforms, people say this is not suitable. What for? My understanding is that you can bind data to almost any control, so that Winforms is missing? I am trying to understand the disadvantages of data binding in Winforms compared to WPF, and why MVVM cannot be used, since it seems easier to implement than MVP.

Thanks in advance Andy.

+7
source share
3 answers

I just checked how data binding in WinForms uses INotifyPropertyChanged . Data binding through BindingSource does support INotifyPropertyChanged if the DataSource of the BindingSource or model corresponding to DataMember implements this. You can make full use of M. Fowlers controlling the lead / controller: you don’t even need handwritten code, BindingSource synchronizes the view with the model parameters in both directions (model β†’ view and view β†’ model), and if the model supports INotifyPropertyChanged , then the view will be update automatically. The code constructs I have used so far are:

  • During view initialization:

    this.bindingSource.DataSource = this.presenter;

  • Code generated by the constructor:

    this.textBoxPhone.DataBindings.Add (new System.Windows.Forms.Binding ("Text", this.bindingSource, "Model.Phone", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));

Model Class:

public class Customer : INotifyPropertyChanged { private string _firstName; public string FirstName { get { return _firstName; } set { if (_firstName == value) return; _firstName = value; NotifyPropertyChanged("FirstName"); } } private string _lastName; public string LastName { get { return _lastName; } set { if (_lastName == value) return; _lastName = value; NotifyPropertyChanged("LastName"); } } private string _company; public string Company { get { return _company; } set { if (_company == value) return; _company = value; NotifyPropertyChanged("Company"); } } private string _phone; public string Phone { get { return _phone; } set { if (_phone == value) return; _phone = value; NotifyPropertyChanged("Phone"); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } 

Presenter Class:

 public class CustomerPresenter { public CustomerPresenter(Customer model) { if (model == null) throw new ArgumentNullException("model"); this.Model = model; } public Customer Model { get; set; } public ICustomerView View { private get; set; } } 
+8
source

Try to find examples of the Supervising Controller MVP flavor, I use this with WinForms, very successfully, I would say. Entities supporting INotifyPropertyChanged, the facilitator associates them with the view, and the facilitator subscribes to the PropertyChanged event so that it knows when the view has changed something (dirty check). View is only responsible for data binding, all other functions are moved to the presentation.

0
source

You will not miss anything. MVVM is very suitable for WinForms. Microsoft only supports the use of WPF and the MVVM template with it.

0
source

All Articles