I think you hit on the two biggest reasons not to use the injector everywhere, but another consideration -- perhaps not focusing on the JSP example explicitly, is that in Guice a "singleton" isn't really a singleton. It's a single instance per injector/application. A lot of times people make singletons and that aren't really a singleton, and if you need to set up an additional "application", it can be useful to contain it all in a single container (a Guice injector). Today you might think that there is a singleton DatabaseConnection but maybe tomorrow you want two connections. Of course if your code is a library or might be used in a library, then it is also critical to avoid static globals. This is what Guice is trying to help prevent.

Jason

On 11/4/2010 6:39 PM, Sondre Bjornebekk wrote:
Sorry to comment on my own post, but I guess a main obligation could
be that the dependency on Guice should not be scattered around the
code base and the practical implication being ease of switching DI
frameworks.  Would still welcome comments (not least on what would be
best practice for the samples given), though. Thanks.

-S-

On Nov 4, 9:07 pm, Sondre Bjornebekk<[email protected]>
wrote:
Hi,

Sometimes it can be very convenient to just do
injector.getInstance(InjectedObject.class), for instance if you have a
object you don't need to be able to mock, but want to send a
dependency that you _would_ be like to be able to mock[1]. Currently I
do this by setting a global reference to the Injector when my module
is set up (typically either in a junit setUp() or extending
GuiceServletContextListener, see bottom for the code for the
latter[2]).

Is this considered bad practice? If so, (a) what is the kosher way?
and (b) any practical consequences?

Also, a somewhat related question:  Instead of doing the assisted
inject hoopla (which Guice admittedly makes much much more elegant
than manual factories), I sometimes find it easier to do:

new NonInjectedObject("param that could have been annotated
assisted",
     injector.getInstance(InjectedObject.class));

Will this be frowned upon by you DI gurus? :-)

Cheers,

-S-

[1] - For example for some simple ad-hoc JSPs, I have a JSPFacade
class that will never be anything else, but it is indeed useful to
switch the implementations it uses or at least get all the
dependencies autowired according to the active module config.

[2] - Code for global reference in the guice servlet context listener:

     /** Don't know if this is bad practice, but sets up and keeps a
global reference that can be used to use the Injector other places. */
     public static Injector getGlobalInjectorReference(){
         return injector;
     }

     @Override
     protected Injector getInjector() {
         injector = Guice.createInjector(new ServletModule() {
             @Override
             protected void configureServlets() {
                 log.info("configureServlets starting");
                 ....
             }
         });

         return injector;
     }


--
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to