TurboManage

David Chandler's Journal of Java Web and Mobile Development

Archive for October 5th, 2009

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…

Posted in Google Web Toolkit, Model-View-Presenter | 7 Comments »

Wrist pain eliminated with keyboard and massage

Posted by David Chandler on October 5, 2009

About three years ago while working on a crunch project and finishing a document on my laptop on the plane, I experienced such wrist pain that I had to hold one hand by the wrist with my other hand and type one finger at a time. That prompted me to do two things:

  1. Start seeing a myofascial massage therapist every 2-4 weeks. This was tremendously beneficial in clearing up the acute pain in my wrists at the time, and has remained beneficial. Every now and then, I just need to get the tension in my arms cleared out completely, and only myofascial massage has been able to do that.
  2. On the recommendation of programmer friends, I started using the Kinesis Advantage Pro keyboard. It took about  a week to become really comfortable with it, but I noticed an immediate and lasting difference. I still experience some forearm irritation (mostly itching), but no more wrist pain. A side benefit of the Kinesisis is that I can type faster than ever before, and much prefer the two-handed Ctrl and Alt key combinations to the dangerous single-handled operations like Ctrl+Tab which I commonly used before.

I of course have a proper chair and sturdy under-desk keyboard tray (the 3M AKT100N). To address shoulder issues, I began switching my mouse from my right to left hand years ago, and now switch mouse hands every day. I use a Kensington Expert Mouse trackball, which I find less likely to cause my wrists to become stiff, and I really like the scroll ring. With the 3M keyboard tray, I can easily switch the mouse tray from one side to the other.

But the main benefit early on came from the Kinesis and massage, and I have thankfully never had a repeat of the acute wrist injury I experienced in 2006. A friend with Liferay told me that the company’s founder bought the Kinesis for all of his employees after the keyboard had saved his own coding career. If you don’t yet have wrist / hand issues but think you might be prone, you should definitely check it out.

I am hopeful that I will eventually be able to eliminate the forearm tension / itching, as well. I exercise 5 minutes with the Flextend glove on an almost-daily basis, and am thereby able to extend the length of time between massage visits.

Yours for perpetual coding,

/dmc

Posted in Ergonomics | 4 Comments »