RCP with Guice

To make my RCP application a little SOA oriented i choose Guice. It’s easy and quick to implement. To call a service you use :

Guice.createInjector().getInstance(aClass);

aClass is not a service but an object using injected services, then you can’t create a ViewPart including service, you must use an intermediate object : a controller.
For example you have a ViewPart (MissionViewPart), a controller (MissionViewController) and a service (MissionService). The declaration look like this :

– MissionService :

@ImplementedBy(MissionServiceImpl.class)
public interface MissionService {
...
}

and create a singleton MissionServiceImpl that implements MissionService.
– MissionServiceImpl :

@Singleton
public class MissionServiceImpl implements MissionService {
    public MissionServiceImpl() {
	...
	}
...
}

– MissionViewController (where the service is injected) :

public class MissionViewController {
    MissionService service;

    @Inject
    public MissionViewController(MissionService service) {
        this.service = service;
    }
...
}

– MissionViewPart

 public class MissionView extends ViewPart {
    public static final String ID = "ResUrgencesSMUR.missionView";
    MissionViewController controller;

    public MissionView() {
        controller = Controllers.getInstance(MissionViewController.class);
    }
...
}

I use a Controllers object because the Guice User’s Guide say :

Your code should deal directly with the Injector as little as possible. Instead,
you want to bootstrap your application by injecting one root object. The container
can further inject dependencies into the root object’s dependencies, and so on
recursively. In the end, your application should ideally have one class (if that
many) which knows about the Injector, and every other class should expect to
have dependencies injected.

– Controllers :

public class Controllers {
    static Injector injector = Guice.createInjector();
    static Map controllers = new HashMap();
    public static  T getInstance(Class type) {
        T controller = (T)controllers.get(type);
        if (controller == null) {
            controller = injector.getInstance(type);
            controllers.put(type, controller);
        }
        return controller;
    }
}

So now when the controller is called by the view, to create UI and bind it and do whatever you want, the service is automatically injected. To use differents service’s implementation you must use the Module concept to define, at the beginning, which implementation have to be uses by each service interface, the @implementedBy key is just the default implementation.
It’s a kind of injection use but if the application is complex and big, and also use a lot of services and differents technologies, i think it’s best to use enterprise SOA frameworks (Improve Foundations, Spring).