java - Can I use some kind of assisted Inject with Dagger? -
with google guice or gin can specify parameter not controlled dependency injection framework:
class someeditor { @inject public someeditor(someclassa a, @assisted("stage") someclassb b) { } }
the assisted parameter stage
specified @ time instance of someeditor
created.
the instance of someclassa taken object graph , instance of someclassb taken caller @ runtime.
is there similar way of doing in dagger?
because factories separate type of boilerplate optimize away, dagger leaves sister project, autofactory. provides "assisted injection" functionality guice offers via factorymodulebuilder, benefits:
- you can keep using autofactory guice or dagger or other jsr-330 dependency injection framework, can keep using autofactory if switch between them.
- because autofactory generates code, don't need write interface represent constructor: autofactory write brand new type compile against. (you can specify interface implement, if you'd prefer, or if you're migrating guice.)
- because type inspection happens @ compile-time, produces plain old java, doesn't have slowness due reflection , works debuggers , optimizers. makes auto library particularly useful android development.
example, pulled autofactory's readme, produce someclassfactory
provideddepa
in @inject
-annotated constructor , depb
in create
method:
@autofactory final class someclass { private final string provideddepa; private final string depb; someclass(@provided @aqualifier string provideddepa, string depb) { this.provideddepa = provideddepa; this.depb = depb; } // … }
Comments
Post a Comment