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

Resolving dependency marked with @Named ? #401

Closed
aryaxt opened this issue Apr 6, 2014 · 5 comments
Closed

Resolving dependency marked with @Named ? #401

aryaxt opened this issue Apr 6, 2014 · 5 comments

Comments

@aryaxt
Copy link

aryaxt commented Apr 6, 2014

I have a class called SomePresenter.
I provide two instances of this class one @nAmed("1") and other @nAmed("2")
In the code I need to resolve this dependency depending on some data and logic, therefore I can't do property injection or constructor injection.

private String type;

public void someMethod() {
   // get an instance of SomePresenter ("1" or "2") depending on the value of "type"
   SomePresenter presenter = objectGraph.get(SomePresenter.class);
}
@jskierbi
Copy link

jskierbi commented Apr 6, 2014

As stated in javadocs, this method throws:

IllegalArgumentException if type is not one of this object graph's injectable types.

As I understand, your SomePresenter is not an injectible type, instead being provided in two named flavours in a module. Correct me if I'm wrong - but it seems that dagger won't let You do that.

@aryaxt
Copy link
Author

aryaxt commented Apr 6, 2014

Thanks for the quick response. Yes, SomePresenter is being provided by my module.
I guess I have to find another way around this.

@tbroyer
Copy link
Collaborator

tbroyer commented Apr 6, 2014

You basically have two options (which are not dagger-specific):

  • if the type with someMethod is managed by Dagger, then inject providers of those 2 flavors and choose the one to get() from depending on the value of type
@Inject @Named("1") Provider<SomePresenter> presenter1Provider;
@Inject @Named("2") Provider<SomePresenter> presenter2Provider;

public void someMethod() {
   SomePresenter presenter = /* use presenter1Provider or presenter2Provider */.get();
}
  • otherwise, create the ObjectGraph with different modules depending on the value of type; those modules will provide SomePresenter without qualifier so you can use objectGraph.get(SomePresenter.class)

@cgruber
Copy link
Collaborator

cgruber commented Apr 6, 2014

I would suggest a slight alteration of Thomas' option-1 code to:

class PresenterEntryPoint {
   @Inject @Named("1") Lazy<SomePresenter> presenter1;
   @Inject @Named("2") Lazy<SomePresenter> presenter2;
}

... but it's basically the same approach. Dagger 1.0 is definitely
designed around having robust entry-point/injectable objects, rather
than using ObjectGraph like a big annotated map.

@aryaxt
Copy link
Author

aryaxt commented Apr 6, 2014

Thanks for the great answers. I took the provider approach.

tbroyer pushed a commit to tbroyer/dagger that referenced this issue Sep 3, 2016
Also stop distinguishing between explicit bindings and delegate declarations during resolution time; it's confusing and unnecessary.

Github: Fixes square#401

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127997438
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

5 participants