Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVP in Winforms

I'm primarily from an ASP.Net background with some MVC. I've also done a little Silverlight and MVVM, however I'm now about to move into Winforms which I have very little experience of, so I'm wondering how to tackle MVP.

Typical MVP samples show the presenter setting a view property (via some kind of IView interface), with the concrete view putting that property value into a textbox for example. Instead of this archaic approach, can one utilise INotifyPropertyChanged in MVP, and if so how? A very quick example would be really useful!

If I was to create a model that implemented INotifyPropertyChanged then isn't this more like MVVM? (i.e. the presenter updates the model, and via the magic of INotifyPropertyChanged the view gets updated). Yet everywhere I've read about MVVM and Winforms, people say it isn't suitable. Why? My understanding is that you can databind just about any control's property, so what's Winforms missing? I'm trying to understand the shortcomings of databinding in Winforms compared to WPF, and why MVVM can't be used, as it seems simpler to implement than MVP.

Thanks in advance Andy.

like image 485
Andrew Stephens Avatar asked Feb 15 '12 21:02

Andrew Stephens


People also ask

What is MVP pattern in C#?

The Model View Presenter (MVP) is a design pattern that is particularly useful for implementing user interfaces in such a way as to decouple the software into separate concerns, such as those intended for data processing and storage (model), business logic, the routing of user commands, etc, thereby making more of your ...

What is the difference between MVC and MVP?

Although the Flow diagram looks same as MVC the difference is how the VIew and Presenters/Controllers interacts with each other. In MVP the Views and presenters interact via an interface(unlike MVC). Presenters perform some action on the Interface, which is implemented in Views and hence the view gets updated.

What is MVP architecture in Android?

MVP (Model — View — Presenter) architecture is one of the most popular architecture patterns and is valid in organizing the project. MVP (Model — View — Presenter) comes into the picture as an alternative to the traditional MVC (Model — View — Controller) architecture pattern.

How does Model View Presenter work?

The view is a passive interface that displays data (the model) and routes user commands (events) to the presenter to act upon that data. The presenter acts upon the model and the view. It retrieves data from repositories (the model), and formats it for display in the view.


1 Answers

I have just checked up how data binding in WinForms uses INotifyPropertyChanged. The data binding through the BindingSource does really support INotifyPropertyChanged if the DataSource object of the BindingSource or model property corresponding to DataMember implements this. You can use M. Fowlers supervising presenter / controller to full extent here: You don't even need a hand-written code, the BindingSource synchronizes the view with the model properties in both directions (model -> view and view -> model), and if the model supports INotifyPropertyChanged then the view will be updated automatically. The code constructs I have used so far:

  1. During view initialization:

    this.bindingSource.DataSource = this.presenter;

  2. Designer-generated code:

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

The 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));
    }
}

The 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; }
}
like image 102
Slawa Konkevych Avatar answered Oct 20 '22 06:10

Slawa Konkevych