Displaying Lists of Data using MVP

Craig Shoemaker has put up some great screen casts at polymorphic podcast that walk through the MVP pattern in the Supervising Controller flavour. The logical question after watching this show, which was also asked of Jean-Paul S. Boodhoo after his screen cast on DNR TV, is how do you pass lists of data between the presenter and the view. Reading Jean-Paul’s response, the thing that seemed a little out of place was the uses of a ListControl, which to my line of thinking has tied the presenter to being used in the web only. I really like Craig’s demo of the same presenter being used for a Web UI and a Web Form UI

Watching the screen casts and thinking about the problem, in the context that Craig has presenting the MVP pattern, I could help but think that the problem should be fairly simple. If you use a list of DTO objects to marshall data between the model and the presenter then I think I have decoupled the view and the model successfully. If I have a list all the view needs to do is set the DataSource and refresh the control.

Our DTO should look like this:

public class ListDataItem { private int _Id; private String _description; public String DisplayMember { get { return _description; } set { _description = value; } } public int ValueMember { get { return _Id; } set { _Id = value; } } }

The presenter then needs to put the data from the model into the DTO:

List<ListDataItem> listData = new List<ListDataItem>(); ListDataItem dataItem; foreach (Winery winery in wineryList) { dataItem = new ListDataItem(); dataItem.ValueMember = winery.Id; dataItem.DisplayMember = winery.Description; listData.Add(dataItem); }

Once the data has been passed to the view getting it into the control is fairly simple:

this.wineryList.DataSource = value; this.wineryList.DisplayMember = "DisplayMember"; this.wineryList.ValueMember = "ValueMember"; this.wineryList.Refresh();

This could be an interesting screen cast for the series Craig?

P.S. Craig I got the Flash Player installed so I could watch the screen casts and thoroughly enjoyed them, great stuff.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: