I've started to wrap my head around the whole MVP pattern and despite I'm doing fine with single objects it starts getting difficult when it comes to collections.
So let's say we're architecting a simple WinForms application that consists of a DataGrid within a Form, with the data Model being a simple collection of stuff, where such stuff has a bunch of properties and the View is going to actually display them:
Model
public class Person
{
public string Name { get; set; }
public DateTime Birth { get; set; }
public bool IsCool { get; set; }
}
public class People
{
public List<Person> Persons { get; set; }
}
View
public interface IPeopleView
{
List<People> ListOfPeople { get; set; }
}
public partial class PeopleViewImpl : Form, IPeopleView
{
private DataGridView _grid = new DataGridView();
public PeopleViewImpl()
{
InitializeComponent();
}
// Implementation of IPeopleView
public List<People> ListOfPeople
{
get { return /* TODO */; }
set { _grid.DataSource = value; }
}
}
Presenter
public class PeoplePresenter
{
private People _model;
private IPeopleView _view;
public PeoplePresenter(People model, IPeopleView view)
{
_model = model;
_view = view;
}
void UpdateView()
{
_view.ListOfPeople = _model.Peoples;
}
}
So what should I implement at View's List<People> ListOfPeople
getter as well as how should I invoke Presenter's UpdateView()
?
And generally, which extra Presenter methods would be interesting to have in order to achieve MVP Passive View and Supervising Controller respectively?
Any advice, code style review or opinion will be sincerely appreciated. Thanks much in advance.
First of all, you should decide for one pattern:
I have collected all aspects as well as links to useful considerations and examples.
In either case, you should define a PeopleModel
and let PeopleViewImplementation
reference PeopleModel
. This clearly separates the model from the view.
When it comes to collections, Supervising Controller can rely on the data binding of a list to the DataGridView
. See winForms + DataGridView binding to a List. Only Passive View and Presentation Model require additional code for mapping the list to the view fields respectively to the Presentation Model.
Secondly, the data mapping should be clarified: Should PeopleView
show a list of persons or a list of several peoples? The DataGridView
can either display one person per row or one people per row. If one person is displayed per row, the treatment of the people could be achieved in one of the following ways, for example: