dagger
dagger copied to clipboard
Optional injections
I have a (somewhat) weird use case: In my app I have two object graphs, one for app-wide and one for activity-wide components, while the latter extends (plusses) from the former.
Now in my instrumentation tests I need to be able to inject all components, though I need to access the app-wide components already before I can inject the activity-wide ones. What I do then is that I call inject() twice, once with the base object graph and once with the (extended) activity graph.
The problem now is of course that the activity-wide components aren't link-satisfyable during the base injection as they're later injected. I know that ButterKnife has an @Optional annotation for such "dependencies", now is there something similar for Dagger as well?
I'm confused. Why would you need to reference your activity-wide objects from the first invocation of your app-wide graph. Or rather, why are you staging tests for activities without the activity-wide graph, which it sounds like you're trying to do.
I think optional in this case would be really unsafe, even if we supported it, as it would end up with your activities being agnostic to the availability of activity-scoped things, which could easily lead to someone forgetting to include something, etc. Can you talk a bit more about your set up, and why you would need this particular organization of code and flow in these tests? I may be missing something.
@cgruber The issue is basically that I have some components / singletons that need to be created and also partially mocked before the activity is created / fully set up, otherwise I suffer from test flakiness. So while I'd really like to just create one, the activity-scoped object graph, and inject stuff from this into my test, the creation of this graph requires a call to getActivity() from Android's instrumentation infrastructure, and unfortunately this does more than just returning some context: it creates and initializes the whole activity synchronously in the background.
Right now I help myself by calling get() on the activity's object graph at the appropriate places and only use annotation-based injection for the base application graph.
I know that ButterKnife has an
@Optionalannotation for such "dependencies", now is there something similar for Dagger as well?
Butter Knife does lookups not injection. With different layout configurations there's a very real possibility some views are present only in certain situations and completely absent in others. It refers to these view lookups as "injection" purely as a joke for RoboGuice converts.
Well, nicely put, point taken :-D
I think Guice' interpretation of @Nullable is then what I'm looking for. See https://code.google.com/p/google-guice/wiki/UseNullable
Yeah - we don't (at present) support @Nullable and nullable providers.
On 7 July 2014 11:12, Thomas Keller [email protected] wrote:
Well, nicely put, point taken :-D
I think Guice' interpretation of @Nullable https://github.com/Nullable is then what I'm looking for. See https://code.google.com/p/google-guice/wiki/UseNullable
— Reply to this email directly or view it on GitHub https://github.com/square/dagger/issues/423#issuecomment-48217282.