Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can Dagger 2 be used to inject using multiple components into the same object

So I have an ApplicationComponent for injecting singletons into my fragments and presenters, but I'm trying to create a component to inject into the same presenter that the AppComponent does. Something along these lines.

@Component{modules = FileManagerModule.class}
public interface FileManagerComponet
{
    public void inject(MyPresenter presenter);
}

@Component{modules = AppModule.class}
public interface AppComponent
{
    public void inject(MyPresenter presenter);
}

@Module
public class AppModule
{
    private Context appContext;
    @Provides
    @Singleton
    public SharedPreferences preferences()
    {
        return appContext.sharedPreferences();
    }
    ...
}

@Module
public class FileManagerModule
{
    private Context activityContext;
    @Provides
    public FileManager FileManager()
    {
        return new FileManager(activityContext);
    }
    ...
}
like image 668
Stampede10343 Avatar asked Aug 16 '16 16:08

Stampede10343


People also ask

How does Dagger injection work?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.

What is Dagger 2 used for?

Dagger is arguably the most used Dependency Injection, or DI, framework for Android. Many Android projects use Dagger to simplify building and providing dependencies across the app. It gives you the ability to create specific scopes, modules, and components, where each forms a piece of a puzzle: The dependency graph.

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.

How does a Dagger work internally?

To understand it better during a basic course, think module as a provider of dependency and consider an activity or the other class as a consumer. Now to supply dependency from provider to consumer we have a bridge between them, in Dagger, Component work as that specific bridge.


1 Answers

To anyone who can't figure this out, one component must provide all the dependencies to an object. So in my case, I'd have to make the FileManagerComponent be a Subcomponent and ".plus()" it with my AppComponent, or make it dependent on AppComponent and have AppComponent expose Context downstream by having a Context context(); Method that will let components that depend on it have access to a the context it has.

For example:

@Singleton
@Component(modules = {NetworkModule.class, AndroidModule.class})
public interface ApplicationComponent {
    FileManagerComponent plus(FileManagerModule module);
}

@Subcomponent(modules = {FileManagerModule.class})
public interface FileManagerComponent {
    void injectMyActivity(MyFileManagingActivity activity);
}

And you would use it like this (in MyFileManagingActivity):

FileManagerComponent fmc = applicationComponent.plus(new FileManagerModule());
fmc.injectMyActivity(this);

Or if you don't want to use subcomponents something like this:

@Singleton
@Component(modules = {NetworkModule.class, AndroidModule.class})
public interface ApplicationComponent {
    Context context();
    File applicationRootDirectory();
}

// Notice this is ALSO a Component
@Component(modules = {FileManagerModule.class}, dependencies = ApplicationComponent.class)
public interface FileManagerComponent {
    void inject(MyFileManagerActivity activity);
}

Now you have to build your component that depends on app component.

FileManagerComponent fmc = DaggerFileManagerComponent.builder()
                   .applicationComponent(appComponent)
                   .fileManagerModule(new FileManagerModule())
                   .build();
fmc.inject(this);
like image 176
Stampede10343 Avatar answered Sep 26 '22 10:09

Stampede10343