-1

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; }
}
Daniel P
  • 123
  • 1
  • 9
  • I learned a new term today, _passive view_! You can just bind the `List` to the DataGridView (or do it through a `BindingSource`). Your code doesn't need to know anything about `T` to do that, the DGV will figure it out. Personally, I'd stay away from you string-ful idea – Flydog57 Jun 19 '21 at 21:27
  • @Flydog57 what type (T) do I pass as List if I'm not supposed to pass a the model to the view? – Daniel P Jun 19 '21 at 22:06
  • I'm not sure what your rules are, but at some point, you're going to need to pass your data to the view somehow. The important thing (I'm assuming) is that your your view is agnostic to T. The `List` class implements `IList`, so use that in the view. The DGV will still figure it out (I think) – Flydog57 Jun 19 '21 at 22:24
  • @Flydog57 Do you use the MVP pattern and do you pass models to your views in your MVP applications? This is my first project where I'm attempting to use the MVP pattern, and AFAIK, the views are not supposed to know about the models. – Daniel P Jun 19 '21 at 22:36
  • Nope, never used MVP. If you don't know anything about your data (for example, its values), how do you present it? What i have written are WinForms apps that allow a user to describe a SQL query (anything executable by the DB) and that I present to the user (think SSMS). In that case, all I know is that I have a collection of results, all of the same type (a type I don't know) – Flydog57 Jun 19 '21 at 22:54
  • I'm going to go ahead and pass the model to the view, I just assumed it wasn't supposed to be passed to the view because every MVP diagram I'd seen before posting showed no dependency between the view and model. A SQL query tool is actually the project I'm working on to query multiple databases from multiple SQL instances at the same time. – Daniel P Jun 20 '21 at 05:43

1 Answers1

1

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.

Kozmocreamer
  • 116
  • 4
  • I think this is what I'll work towards if I don't scrap the Passive View method altogether and adopt the Supervising Controller method. With Supervising Controller it's OK for the presenter to pass the model to the view for the purpose of binding. That said, the ability to edit bindings from the designer in VS 2019 and VS 2022 Preview 1 is broken in .Net 5/6 for winforms projects, so that's kind of ruled out for now unless I hand edit the designer. – Daniel P Jun 24 '21 at 22:05