Responsibility question

Topics: Prism v2 - Silverlight 2
Jul 5, 2009 at 4:36 PM

I have a question regarding code responsibility in the MVP pattern.

Say I have a view that holds a DataGrid, and a presenter that is responsible for attaching the view to a model, the view then takes this model and sets it as the DataGrid's ItemSource. I hope so far it makes sense.

Now the issue is, the data that is attach is polymorphic, and according to the type, the DataGrid should display different column, not using the standard AutoGenerateColumns, but rather a custom code we have.

The question is, whether the decision of which column to use and how to create them should be the View's responsibility (because he knows the DataGrid and how to interact with it) or should it be the Presenter's responsibility because he has the business-view logic of which columns should be displayed for each type, but doesn't (shouldn't?) know how to interact with the DataGrid - since View's DataGrid can be replaced in the future with other data grids (3rd party).

Is it the View's or the Presenter's responsibility? or is it a combined effort? (for example, the presenter creates a special collection of column information and the view applies it to the data grid, using the control's specific properties and methods)

I'd appreciate your design input.

Thanks in advance,

Ido.

Jul 7, 2009 at 6:16 PM

Hi Ido, 

The three options you explain are perfectly feasible and which is the best might greatly depend on you specific scenario. More over there are several variations of the MVP pattern.
There are some documents and articles that provide some advice:

 I will provide some more information, that expresses my personal point of view.

As the prism-v2 documentation states for the Presentation Model pattern, the presentation model should be designed to be easily consumed by a view. "Presentation model also frequently adapts the underlying model data into a form that is easier to represent in the user interface". So, if you are using this pattern your 3rd option seems to fit better: a combined effort, where the presenter exposes the model in such a way that the view can easily consume. This way the logic of which columns to show will be in the presenter but the view will be in charge of showing them (encapsulating the specific control). 

If your scenario allows for it, another approach might be using Data Templates. In WPF, you can specify the data template for an object type at the application level. In Silverlight, you have to explicitly specify the data template for an object within the control that is to display it. Julian Dominguez as posted a good example of how to use dataTemplates with prism: Presentation Model with DataTemplates in CompositeWPF (Prism) Sample. 

Hope it helps! 

Matias Bonaventura
http://blogs.southworks.net/matiasb