Guice style service locator
Has anyone ever seen / tried to write a service locator template that uses a Guice style config system?
I currently have a GWT project (which uses GWT-RPC) that uses a command pattern where my RPC servlet looks like this ...
public interface TransactionService extends RemoteService {
<T extends Response> T execute(Action<T> action);
}
In my current implementation of the execute method, I am doing this ...
if(action instanceof SomeActionImpl){
doSomeActionImpl((SomeActionImpl)action);
}else if(action instanceof SomeActionImpl2){
doSomeActionImpl2((SomeActionImpl2)action);
}
What I would like to do is figure out a way to get rid of the giant if statement. I need to register somehow that the ActionImpl1 class should be delegated to another TransactioNService implementation.
Any ideas? I thought of just adding entries to the HashMap where the key is the Action class and the value is the ServiceImpl class. One I have a link to the ServiceImpl class, I could use Guice to get an instance of the TransactionService.
source to share
Take a look at the net.customware.gwt.dispatch.server.DefaultActionHandlerRegistry class in gwt-dispatch ( http://code.google.com/p/gwt-dispatch/ ); it does exactly what you suggest. Here is the member variable that holds the handlers:
closed final map <Class <? extends action <→, ActionHandler <>, → → Handlers;
If you want to execute server-side handlers, use gwt-dispatch server-side components; if this is client stuff, then consider modeling your dispatch class in the DefaultActionHandlerRegistry.
source to share