Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 - modules from different components

I am not quite sure how to solve this with dagger 2. Lets assume we have ApplicationModule that provides us ApplicationContext then we have ApplicationComponent that uses just this one module. Then on top of it we have ActivityModule and ActivityComponent that has dependency on ApplicationComponent. ActivityComponent is build just like

    ApplicationComponent component = ((MyApplication) getApplication()).getComponent();

    mComponent = Dagger_ActivityComponent.builder()
            .applicationComponent(component)
            .activityModule(new ActivityModule(this))
            .build();

And then I inject my activity:

mComponent.inject(this);

Now I am able to use everything that is declared inside my ActivityModule, however it is not possible for me to access ApplicationModule.

So the question is how could that be achieved? So that when I build component that depends on another component I can still access module from the first one?

EDIT

I think I have found solutions, after rewatching Devoxx talk by Jake again, I have had to miss that out, whatever I want to use from another components module I have to provide in that component, for example I want to use Context from ApplicationModule then inside ApplicationComponent I have to state Context provideContext(); and it is going to be available. Pretty cool :)

like image 766
user3274539 Avatar asked Jan 20 '15 13:01

user3274539


People also ask

What are the components in Dagger 2?

Components are essentially the glue that holds everything together. They are a way of telling Dagger 2 what dependencies should be bundled together and made available to a given instance so they can be used. They provide a way for a class to request dependencies being injected through their @Inject annotation.

What is component and module in Dagger?

For a fast read, a Component in Dagger is an unit which is essentially used to resolve dependencies. A Module is a sub-unit, providing dependencies, one or several of which can be used by a Component. A SubComponent is a type of Component which derives from a Component, inheriting the dependencies it already provides.

Is Dagger deprecated?

It's officially deprecated and you can pretty much ignore it. Google's framework, which became dominant in Android ecosystem, was originally called Dagger 2. Sometimes we still refer to it as such, but, in most cases, we simply call it Dagger today.

What are modules in Dagger?

A Dagger module is a class that is annotated with @Module . There, you can define dependencies with the @Provides annotation. // returns (i.e. LoginRetrofitService). // Function parameters are the dependencies of this type.


1 Answers

You have already answered your question, but the answer is to specify the provision methods in your "superscoped" component (ApplicationComponent).

For example,

@Module
public class ApplicationModule {
    @Provides
    @Singleton
    public Something something() {
        return new Something.Builder().configure().build(); 
           // if Something can be made with constructor, 
           // use @Singleton on the class and @Inject on the constructor
           // and then the module is not needed
    }
}

@Singleton
@Component(modules={ApplicationModule.class})
public interface ApplicationComponent {
    Something something(); //PROVISION METHOD. YOU NEED THIS.
}

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}

@ActivityScope
public class OtherThing {
    private final Something something;

    @Inject
    public OtherThing(Something something) {
        this.something = something;
    }
}

@Component(dependencies = {ApplicationComponent.class})
@ActivityScope
public interface ActivityComponent extends ApplicationComponent { //inherit provision methods
    OtherThing otherThing();
}
like image 83
EpicPandaForce Avatar answered Sep 24 '22 16:09

EpicPandaForce