Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Optional injections #423

Open
realdadfish opened this issue Jul 3, 2014 · 5 comments
Open

Optional injections #423

realdadfish opened this issue Jul 3, 2014 · 5 comments

Comments

@realdadfish
Copy link

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?

@cgruber
Copy link
Collaborator

cgruber commented Jul 5, 2014

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.

@realdadfish
Copy link
Author

@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.

@JakeWharton
Copy link
Collaborator

I know that ButterKnife has an @Optional annotation 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.

@realdadfish
Copy link
Author

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

@cgruber
Copy link
Collaborator

cgruber commented Jul 7, 2014

Yeah - we don't (at present) support @nullable and nullable providers.

On 7 July 2014 11:12, Thomas Keller notifications@github.com 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
#423 (comment).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants