google-code-export / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
2 stars 1 forks source link

Another way to assist injections #419

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
If I'm wrong please reject this issue.

As I understend Guice looks with reflection for appropriate consructor 
when I call injector.getInstance(clazz).

How about to implement injector.getInstance(clazz, var-args of manual 
parameters)? 

In this situation guice should try to find any constructor that matches 
passed manual parameters + injected dependencies. It can be done 
heuristically (as a bouns params order shoud't be considered) or such 
params should by marked with some annotation (like @Assisted one). 

If guice could do that I think there would be much less situations for 
writing boilerplate providers.

Or promoting injector as "super" provider for user classes is a wrong way?

Original issue reported on code.google.com by kua...@gmail.com on 31 Aug 2009 at 2:42

GoogleCodeExporter commented 9 years ago
One of the main deciders on this issue is whether or not assisted inject should
remain an extension, or be promoted to core guice.  If not the latter, modifying
Injector to support an extension may not fly.

Original comment by fernc...@gmail.com on 31 Aug 2009 at 5:51

GoogleCodeExporter commented 9 years ago
I think more important than that we want to discourage people from using 
Injector.getInstance() and instead 
prefer viral dependency injection (@Inject everywhere).

For example, in the GS2 programming model, you never call getInstance() from 
anywhere and never deal with 
the injector. It sits in the background doing all the work, while you simply 
use your services as they are needed.

Original comment by dha...@gmail.com on 31 Aug 2009 at 11:44

GoogleCodeExporter commented 9 years ago
You can use the AssistedInject extension to provide parameters to dependencies. 
 The
Injector will not support this because it makes it much more complex to figure 
out
the dependency chain and is overall a bad thing for understanding code.  More 
than
that, it is a bad practice to inject the Injector, and this would require that.

Original comment by sberlin on 2 May 2010 at 12:30