So Long MVP
A while back, Martin Fowler decided to retire (at least from his world) the Model View Presenter pattern. Specifically he’s split it into two new patterns called Supervising Presenter and Passive View.
Those familiar with MVP as applied to the CAB world (especially if you’ve seen the Smart Client Software Factory), will recognize Passive View as the pattern called MVP. There’s no data-binding in the reference implementations.
Supervising Presenter is more accommodating toward data-binding, which is good as it’s often times a very desirable thing to do (grids, etc.) I think it’ll become even more desirable with in the WPF world…
The view interface in this scenario would define events (or methods) which are often already implemented by your underlying technology — in the case of System.Windows.Forms.Form’s & System.Web.UI.Page’s “Load” event. The presenter defines handlers for these events which can easily be wired in its setter dependency for the view interface, e.g.
public class MyPresenter
{
private IMyView _view;
public IMyView View
{
set
{
_view = value;
_view.Load += OnViewLoad;
}
}
public void OnViewLoad(object sender, EventArgs e)
{
//.. do business logic, act on the view, etc.
}
}
public interface IMyView
{
event EventHandler Load;
event EventHandler Save;
}
A slight departure from the normal MVP style of handling the event in the view and calling back to a corresponding method in the presenter (although I guess more-or-less the same effect). I’d favor this approach when using third party controls to reduce coupling… e.g. keep the event delegates simple EventHandlers. Of course, there’s nothing preventing one from using this model: calling back to a presenter method from an event handler defined in the view.
The point with Supervising Presenter is that only the business logic is handled by the presenter. We assume the underlying databinding goes to plan. So if we want to color a specific databound textbox (say), we put that logic (choosing the color based on values present in the model) inside the presenter… and test it.
Here’s a complete code sample covering Supervising Presenter from Phil Haack. Note he doesn’t use an interface. Not my preferred approach; reuse aside, a view interface makes mocking easier. I, like Phil, am not a big believer in reusable presenters (e.g. same presenter for ASP.NET and Smart Client). If you’re looking for reuse in this way, I think you really have to question just why it is that you’re duplicating your UI for two platforms. Sometimes there is a case for this (think Outlook/OWA), but I think it’s fairly rare… at least in my experience. If you need reuse better to factor back into your business/services layers.
—-
Martin Fowler
Retirement note for Model View Presenter
Supervising Presenter & Passive View
Post a Comment