Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT, MVP, and UIBinding - How to get the best of all worlds

Tags:

mvp

gwt

With MVP, you normally bind the View (UI) with the Presenter in the Presenter. However with the latest version of GWT, especially with UIBinding, you can do the following in the View:

@UiHandler("loginButton")
void onAboutClicked(ClickEvent event) 
{
    // my login code
}

Which basically exchanges a lot of anonymous inner class code for some quick annotation code. Very nice!! The problem is that this code is in the view and not the presenter...

So I thought maybe:

@UiHandler("loginButton")
void onAboutClicked(ClickEvent event) 
{
    myPresenter.onAboutClicked(...);
}

But there are several problems with this approach. The most important, you blur the lines between View and Presenter. Who does which binding, in some cases it's the View, in others it's the presenter (binding to events not in your current view but that need to be attached - for example a system wide update event).

You still get the benefit of being able to unit test your presenter, but at what cost. The responsibilities are messy now. For example the binding is sometimes in the View and others times in the Presenter level. I can see the code falling into all kinds of chaos with time.

I also thought of extending the Presenter to the View, so that you could do this in the View. The problem here is that you lose the Presenter's ability to run standard unit tests! That's a major issue. That and the lines again become blurred.

So my question, does anyone have a good method of taking advantage of the annotation from UIBinding within the MVP pattern without blurring the lines and losing the advantages of the MVP pattern?

like image 787
Stephane Grenier Avatar asked Mar 15 '10 16:03

Stephane Grenier


1 Answers

I tend to use the @UiHandler approach only when the Handler does view-specific stuff, like adding style names, etc. For other purposes (adding validation, etc), I stick with the old approach of adding the appropriate Handlers in the Presenter.
I'd recommend reading through one of the many threads on GWT Google Group about MVP and UiBinder, such as this one.

like image 50
Igor Klimer Avatar answered Sep 18 '22 07:09

Igor Klimer