I've run into the same issue, and every time I've looked at AssistedInject
it seemed boilerplate-heavy and kludgey. It also doesn't scale well
code-wise if you either have a lot of things you'll want to inject that
way, or if you want to write a framework that allows its users to provide a
list of injectable types and pass in objects to inject.
If what you're writing has some sort of natural request/response scope, the
generic solution I've used is custom scopes. It doesn't change the fact
that *something* at the front of your call stack has to know how to derive
the value of "name", but that's true of AssistedInject too, so the idea
that it's more decoupled one way than the other is an illusion - it's just
a question of, would you rather write the boilerplate of assisted inject or
class OuterThing {
private final ScopeRunner runner;
@Inject
OuterThing (ScopeRunner runner) {
this.runner = runner;
}
public void onRequest () {
String name = ...
try (AutoCloseable ac = scope.enter(name, otherThing, whatever)) {
runner.run (R.class);
}
}
private static class R implements Runnable {
private final Dep dep;
@Inject R(Dep dep) { this.dep = dep; }
public void run() { ... }
}
}
The more "live" things you have that can usefully be injected, the more
this approach pays off. Yes, something has to touch the injector
post-startup, but that's happening anyway with AssistedInject, and the real
value is in having a clean programming model.
http://timboudreau.com/builds/job/Useful-Netty4-Branch/lastSuccessfulBuild/artifact/guicy/scopes/target/site/apidocs/index.html
https://timboudreau.com/code/useful/file/tip/guicy/scopes<https://timboudreau.com/code/useful/file/3e8cb0f13357/guicy/scopes>
-Tim
--
You received this message because you are subscribed to the Google Groups
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.