TurboManage

David Chandler's Journal of Java Web and Mobile Development

  • David M. Chandler


    Web app developer since 1994 and former Developer Advocate with Google now residing in Colorado. Besides tech, I enjoy landscape photography and share my work at ColoradoPhoto.gallery.

  • Subscribe

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 241 other followers

  • Sleepless Nights…

    October 2009
    S M T W T F S
    « Sep   Nov »
     123
    45678910
    11121314151617
    18192021222324
    25262728293031
  • Blog Stats

    • 876,139 hits

Factoring out common MVP services into a Service Facade

Posted by David Chandler on October 5, 2009

Shortly after adopting the Model-View-Presenter pattern, I found myself copying the same service call into multiple presenters, as both a navigation panel and a main content area can sometimes display the same information. I decided to factor out the repeated service calls into a Service Facade (for lack of a better name) and inject the facade into each presenter that’s needed.

	@Inject
	public AddPrayerListPresenter(Display display, EventBus eventBus, PrayerListServiceFacade prayerListServiceFacade)
	{
		super(display, eventBus);
		this.prayerListServiceFacade = prayerListServiceFacade;
		bind();
	}

Inside the service facade, I use constructor injection to get the event bus and dispatcher the same as in a presenter.

	@Inject
	public PrayerListServiceFacade(final EventBus eventBus,
			final DispatchAsync dispatch, RoaModel roaModel)
	{
		this.dispatch = dispatch;
		this.eventBus = eventBus;
		this.model = roaModel;
	}

	public void refreshPrayerLists()
	{
		GWT.log("Calling actionFindLists", null);
		// Fire event that marks start of the service call so listeners
		// can show the AJAX wait thingy if desired
		eventBus.fireEvent(new RefreshingPrayerListsEvent());
		dispatch.execute(new FindPrayerListsAction(),
				new AsyncCallback<FindPrayerListsResult>()
				{
					@Override
					public void onFailure(Throwable e)
					{
						GWT.log(e.getMessage(), e);
						Window.alert(e.getLocalizedMessage());
					}

					@Override
					public void onSuccess(FindPrayerListsResult result)
					{
						eventBus.fireEvent(new PrayerListsModifiedEvent(result.getPrayerLists()));
					}
				});
	}

The refreshPrayerLists() method contains the code that had been common to multiple presenters. There is one difference, however. When a presenter calls a service, it typically passes a DisplayCallback object, which gwt-presenter uses to call the view’s startProcessing() and stopProcessing() methods so you can show an Ajax wait thingy (er, progress indicator?) I could add a Display argument to the service method and create a DisplayCallback as usual; however, in this case, I want the refresh method to be called only once in conjunction with the event that necessitates a refresh (say, a new prayer list is added). And I want multiple widgets to be notified when the service call begins, and when it finishes. To accomplish this, I pass only a regular AsyncCallback object and fire an event both before and after the service call, to which any interested widget can listen.

In summary, the service facade provides a way to group all related service calls in one place and to factor out common calls from multiple presenters. Now all I need is a better name for it, as it sounds a little scary for my taste…

Advertisements

7 Responses to “Factoring out common MVP services into a Service Facade”

  1. Now all I need is a better name for it, as it sounds a little scary for my taste…
    I use a similar pattern on my project. I just call it a *Service and *ServiceImpl – allowing me to mock it out when writing tests with EasyMock.

    • Thanks, Matt. I had wanted to avoid confusion with GWT-RPC services, but come to think of it, with gwt-dispatch, there’s only one real service, so there won’t be any naming confusion, after all. And the subtle suggestion to use an interface also is well-taken.

  2. Darren said

    Very neat. I’m finding your blog a very interesting read. 🙂

  3. Roar Skullestad said

    Just curios, how is RoaModel implemented? I see from another post that it contains a getLoginInfo() method. How does it use DispatchAsync? Does it always contact server or does it work like a cache?

  4. lost said

    what exactly is a PrayerList ?!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: