google-code-export / google-guice

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

[RFE] Allow type inference in assisted injection #422

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
As written in a mail to the mailing list, it would be nice that Guice's
assisted injection infer the type we want like Guice (core) does.

For instance we could have the following class structure:

public interface MyInterface { }
public class MyClass implements MyInterface {
  public MyClass(@Assisted Integer i) { }
}
public interface MyFactory {
  public MyInterface create(String s);
}

Then a mapping like this one in a module:

@Override public void configure() {

bind(MyFactory.class).toProvider(FactoryProvider.newFactory(MyFactory.class, 
MyClass.class));
}

@Provides Integer provideInteger(String s) {
  return Integer.parseInt(s);
}

The use case is that I have a builder that uses the factory and that has
one member that is not a member of the factored class but rather a derived
or computed value of it. And as the best practices say: "inject only direct
dependencies".

Original issue reported on code.google.com by ogregoire on 3 Sep 2009 at 8:26

GoogleCodeExporter commented 9 years ago
If you would like fancier abilities in your factories, such as converting the 
factory
parameter to something else, consider manually implementing your factory to do 
it &
constructing the dependent class.  Injecting a factory is an OK solution as 
opposed
to injecting the "direct dependency".  The factory is essentially your direct
dependency, because it's required to get at the dependency.

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