Search code examples
c#winformsmvppassive-view

MVP Pattern - Populating lists and grids in passive views


Let's say my presenter obtains a list of my Person class from my repository and I want to bind information from that list to a ListBox or DataGridView in a passive view.

Since the view should not know about the model, would I be correct in assuming I would need to convert that list into a List< string > in my presenter and pass that to the view to bind to a ListBox?

What should I pass to the view if I wanted to populate a DataGridView, a List<List< string >> perhaps?

Would it be acceptable to have a model specifically made for the view to bind to, where the presenter converted the model from the repository into a different model for the view?

Example Person model:

public class PersonModel
{
    public int PersonId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleInitial { get; set; }
}

Solution

  • If you are using true MVP you would within your Presenter take your list of DataModels and map them to a list of ViewModels so that there is still a separation of concerns between your data and view layer. So just make a ViewPerson.cs class in a Models folder on your client copying the same properties as your data model, then use an AutoMapper or a custom map method to map each one.