Parameters for View to Presenter Communication

I’m a big time fan of the Passive View pattern.  I find that in terms of allowing for testing of the UI, Passive View allows me to get as close to the user as possible. 

While there are a few ways to handle communication in Passive View my preference is on direct references.  By this I mean the Presenter has a reference to an interface that the View implements and the View has a reference to the Presenter.  I don’t use an interface for the Presenter because honestly in all my time I’ve never actually seen the Presenter be replaced (which after testing is the main reason to use an interface imho).

A common action in my current project is to have the user select an item from a grid so we can show more detail.  My current modus operandi is to use a screen specific DTO to pass information from the service back to the UI.  If I’m displaying the information in a grid I just bind the DTO to the grid control. 

The question becomes when the view needs to call the presenter how does it do it? 

I was recently working on a piece of existing code that was passing the entire DTO back to the Presenter.  I ended up changing this so that the View instead just passed back the ID.  The reason for this is that by passing back the entire DTO we had created an expectation that the View would always have access to the original DTO.  What if we switched to a grid that just let us store values with a linked id? 

Here is a very simple example of the result…

   1: public partial class Order : Form, IOrderView
   2:     {
   3:         private IOrderPresenter m_Presenter;
   5:         // Code that linked everything together
   7:         public void OrderSelected()
   8:         {
   9:             int orderId = GetCurrentlySelectedOrderId();
  11:             m_Presenter.OrderSelected(orderId);
  12:         }
  14:         private int GetCurrentlySelectedOrderId()
  15:         {
  16:             // Do some magic with whatever Grid control we are
  17:             // currently using to get the selected row.
  18:         }
  19:     }
  21:     public class OrderPresenter : IOrderPresenter
  22:     {
  23:         private readonly IOrderView View;
  25:         // Code that linked everything together
  27:         public void OrderSelected(int orderId)
  28:         {
  29:             // Do something with the selected order.
  30:         }
  31:     }

At the end of the day even though I treat my Presenter and View as a unit I always try to make sure that the View has as much freedom as possible.  The actual UI components we use are likely to place requirements on us that we have very little to no control over.

posted @ Friday, March 13, 2009 11:05 PM

Comments have been closed on this topic.