locked
How to populate a ListView using Model View Presenter RRS feed

  • Question

  • I have inherited an application with a lot of code in the user interface classes. I have managed to refactor some of the code into a Model View Presenter pattern but am at a loss in how to deal with detailed list views.

    I am particularly interested in how to populate a detailed list view using the Model View Presenter pattern without having references to Windows Forms in other than the Views. The content for the list view is retrieved from a database.

    I am using Visual Studio 2008 Professional on a Windows 7 Workstation and retrieving data from MS Sql Server 2008. 

    Can anyone help?

    Thank you.

    Sunday, April 4, 2010 10:51 AM

Answers

  • You're correct. The MyDataItem needs to be exposed to the view as 'view data'.

    If you don't want to expose that class, you need some other class that provides the same data (and maybe some additional display-specific data). The Presenter is then reponsible for creating that view-data based class based on the real MyDataItem.

    You could check out WPF Apps With The Model-View-ViewModel Design Pattern. MVVM is essentially the same as Presentation Model. PM has been defined by Martin Fowler, MVVM by Microsoft. The only difference is that in MVVM they use the PropertyChanged event (part of INotifyPropertyChanged) for notifying the view, instead of specific events per property.

    • Proposed as answer by DennisDoomen Friday, April 9, 2010 6:26 PM
    • Marked as answer by stigbenning Friday, April 9, 2010 9:27 PM
    Friday, April 9, 2010 6:26 PM

All replies

  • Your Presenter will retrieve the data from some data source and pass it on by calling the IView.DisplayItems(IEnumerable<MyDataItem>) (or something like that). Typically these take the form of some class that represents one row of data. The view (code-behind) will implement that method and pass on the data to the list view.

    BTW, if the reason for using MVP is to easily allow unit testing your UI logic, I would consider using the Presentation Model pattern (or the MVVM pattern).

    Sunday, April 4, 2010 1:00 PM
  • Hi DennisDoomen,

    Thank you for your answer and suggestions.

    As to your answer: If I should use your suggestion wouldn't it require that the View knows about the MyDataItem class? If not I would have to implement a class similar to the MyDataItem class and let that do the work. I would prefer that the View remains in the dark concerning the MyDataItem class.

    But maybe I don't understand clearly how you see it done? If that is the case please bear with me and explain in more detail. Thanks.

    I am interested in the two other patterns you mention. Do you know some good links to (prefarably simple) examples in C# for the Presentation Model pattern?

    On beforehand thank you.

    Sunday, April 4, 2010 1:32 PM
  • You're correct. The MyDataItem needs to be exposed to the view as 'view data'.

    If you don't want to expose that class, you need some other class that provides the same data (and maybe some additional display-specific data). The Presenter is then reponsible for creating that view-data based class based on the real MyDataItem.

    You could check out WPF Apps With The Model-View-ViewModel Design Pattern. MVVM is essentially the same as Presentation Model. PM has been defined by Martin Fowler, MVVM by Microsoft. The only difference is that in MVVM they use the PropertyChanged event (part of INotifyPropertyChanged) for notifying the view, instead of specific events per property.

    • Proposed as answer by DennisDoomen Friday, April 9, 2010 6:26 PM
    • Marked as answer by stigbenning Friday, April 9, 2010 9:27 PM
    Friday, April 9, 2010 6:26 PM
  • Once again thank you. I have been thinking along these lines myself and will mark your reply as answer.

    Thank you also for the useful link. I have been around that page before but skipped it because it was based on WPF which would be too much work since the application I have inherited is quite big and since I therefore "only" will pull the functionality in the user interfaces out into testable modules in small steps. I will study the page at the end of the link more though since it should be possible to use the same pattern for non WPF based applications.

    Best regards

    Friday, April 9, 2010 9:27 PM