Guice injection and RequestFactory: Extending ServiceLayerDecorator
I searched for a solution to use Guice Dependency injection together with RequestFactory
.
I stumbled upon this: https://github.com/etiennep
It wasn't working for me so I changed the InjectedServiceLayerDe开发者_C百科corator.java implementation to this:
https://github.com/opncow/injected-requestfactory/blob/master/src/main/java/com/trycatchsoft/gwt/requestfactory/InjectedServiceLayerDecorator.java
Now my questions are:
Can something be done better regarding the caching mechanism of RequestFactory
(Is it still working?)?
What is getTop()
and getNext()
(in ServiceLayerDecorator) for?
And is it correct / safe to use getTop()
in this place?
Sorry thought too complicated! It was as easy as:
Class<?> serviceClazz = resolveServiceClass(requestContext);
return injector.getInstance(serviceClazz);
What is getTop() and getNext() (in ServiceLayerDecorator) for?
ServiceLayer uses a chain of responsibility pattern: in cases your decorator has nothing specific to do, it should delegate to the next decorator in the chain (returned by getNext
) by calling the same method with the same arguments. If your decorator changes the arguments, or needs to call another method, it should call it on getTop
so the call is routed through all decorators, and not just the ones after itself in the chain.
Your use of getTop
is thus correct and safe (have a look at the LocatorServiceLayer from GWT, that's exactly what it does).
But your code (and Etienne's one!) can actually be made simpler and better: simply override createServiceLocator
to get an instance from your injector (same as createLocator
).
精彩评论