Here's an example of why I don't "get" Guice.
class A {
@Inject B b;
}
class B {
@Inject C c;
publc void useC() {
c.doSomething();
}
}
If you use an injector to get an instance of A, it doesn't Inject the
member c in class B. This is my main problem with Guice.
I know the pattern is to do this:
class B {
C c;
@Inject
public B(C c) {
this.c = c;
}
publc void useC() {
c.doSomething();
}
}
But that is unacceptable. I don't want Guice to handle the creation
of B, I just want it to provide C to B without using the constructor.
So the other solution is to use a static field.
class B {
@Inject static C c;
publc void useC() {
c.doSomething();
}
}
But now you have to add binder.requestStaticInjection(B.class) to your
module. If I use C in a lot of classes, I don't want to manually add
each requestStaticInjection to the module!
I don't understand why Guice cannot inject C in my original example,
or if its possible to use Guice to make it work without having to
modify the constructor or dozens of lines of requestStaticInjection.
In my head, this is how I think Guice should work. Guice uses a
custom classloader such that when it sees @Inject, it checks the
module for the right class. You can still run through the classpath
to check that you have something bound to all members that use @Inject
and this can be cached to file too!
Then this way you don't ever need to use
injector.getInstance(RootClass.class), you just new a RootClass, or
any class for that matter.
If Guice wants us to add members to the constructor for them to be
inject, then I feel there's not point in using Guice. It is not more
difficult for me to keep a reference to C and pass it to B's
constructor myself.
Am I missing something to "get" Guice?
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---