Friday, January 22, 2010

Are MVP and AJAX a good match?

Can't seem to get through a conversation these days without talking about the MVP pattern - so here's a boring post about just that.

If you need extensive unit testing coverage, adopting the MVP pattern in your app is probably going to make your life easier. In the specific case of web apps though there are a number of considerations to take into account.

First of all, not everyone seems to be aware that MVP was retired a while ago, and it has been split by the author in two different patterns: Passive View and Supervising Controller. In my experience, when talking about MVP, most people implicitly refer to the Passive View model.

Passive View looks like a pretty good fit for the ASP.NET post-back model, where your presenter triggers updates on the view server-side and everyone can feel comfortable they're following the pattern. This is all good till you start sneaking loads of AJAX calls into your page, resulting in a view that has got now inherent behavior (in this discussion I am considering all the client-side scripting as inherent behavior of the view - which might be a wrong assumption, if so please slap me hard). Still nothing wrong, till the AJAX calls from the view go straight down to the model to fetch data which is used by the view to update itself. Bare in mind that here the problem is not so much the view that updates itself, but the fact that, going straight to the model, the view is no longer passive and the presenter doesn't trigger the update (as we obviously don't have a client-side equivalent).

The considerations above seem to rule out the passive view approach for AJAX intensive apps. In fact, in the Passive View pattern the view is only allowed to talk to the presenter (usually through some messaging mechanism). All this to say that if you're adopting MVP in a web project that requires AJAX calls from the client representation of the view to the service layer (the model), you better specify you're not adopting Passive View, because - at all effects - you're not!

But maybe not all is lost.

If Martin Fowler makes a clear distinction between passive view and selective controller there must be a reason (and a pretty good one for sure). In fact, in a scenario where the view selectively talks to the model and updates itself and "the controller/presenter defers as much as it is reasonable to the view", you're basically adopting the Supervising Controller variation of the MVP pattern (even if you don't know). This scenario is particularly well suited in case of databindings, and even if this is not strictly true in the case of loads of AJAX calls down to the model, I would probably see this AJAX scenario a good fit for this variation of the pattern (compared to passive view).

I find it might be easier for people to get it right if they feel they're closely following the pattern - this is definitely true for me!

So in answer to the title question: Are MVP and AJAX a good match?
I think they might - as long as you're not talking about Passive View!


ImaginaryDevelopment said...

maybe I've misunderstood something... but my ajax calls all call methods on the presenter/controller. so that it is not talking to the service/model layer. Additionally in at least one case, and we may move this direction in more of the ajax, I'm returning a partial view. Did I miss the point and I'm actually not doing passive?

Johnny Idol said...

@ImaginaryDevelopment: From the sound of this you're actually close to passive, or at least your view doesn't call into the model (but only into the presenter) avoiding the main argument I have against an MVP implementation not being Passive View.

Said so, if your AJAX is calling into the presenter, you're most likely calling into static methods, which are returning something to the view and the view inherent behavior (javascript?) knows how to handle that something, meaning that the View is driving its own updates ... which leads me to think still this is not a 'true' passive implementation.

This leads me also to conclude - as stated in the post - that for situations such as this (ajax web apps) the supervising controller model it's a better fit (because you can call into the model all you want as long as it's 'deemed appropriate').

Keep also in mind that I am no guru - and I am figuring out this stuff as much as yourself. These is at the moment my opinion - but I would welcome anyone who could challenge it with something that makes more sense!

ImaginaryDevelopment said...

Cool, I'm no guru either, as MVC is the only one I've got any experience or knowledge about.