With Google Guice or Gin I can specify parameter with are not controlled by the dependency injection framework:
class SomeEditor {
@Inject
public SomeEditor(SomeClassA a, @Assisted("stage") SomeClassB b) {
}
}
The assisted parameter stage
is specified at the time an instance of SomeEditor
is created.
The instance of SomeClassA is taken from the object graph and the instance of SomeClassB is taken from the caller at runtime.
Is there a similar way of doing this in Dagger?
Because factories are a separate type of boilerplate to optimize away (see mailing list discussion here), Dagger leaves it to a sister project, AutoFactory. This provides the "assisted injection" functionality Guice offers via FactoryModuleBuilder, but with some extra benefits:
Example, pulled from AutoFactory's README, which will produce a
SomeClassFactory
withprovidedDepA
in an@Inject
-annotated constructor anddepB
in acreate
method:Yes, please check this Square project: square/AssistedInject
Currently it is not in 1.0 yet for purpose. They wait until Dagger will introduce a public API for registering those generated
Module
classes automatically - see this issue. With that you won't have to reference them in your Dagger code as in this example from README: