2

I have a simple situation here which I am not able to get around (since today is my second day with dagger).

I have a RepositoryManager class the intent of which is to house all repositories like(StudentRepo , TeacherRepo etc etc)

This is my RepositoryManager

public class RepositoryManager {

private static RepositoryManager INSTANCE = null;


@Inject
StudentRepo studentRepo;
@Inject
TeacherRepo teacherRepo;

private final List<Repository> repositories;
private Context context;

@Inject
public RepositoryManager(Context context) {
    this.repositories = new ArrayList<>();
    this.context = context;
    addRepositories();
}

private void addRepositories() {
    addRepository(studentRepo);
    addRepository(teacherRepo);
}

I understand why my studentRepo and teacherRepo are null here. It is because I have not asked Dagger to fetch them for me . I believe I am missing some very important aspect of Dagger here in which we can explicitly fetch instances of our desired objects.

The StudentRepo btw has its own module and I can easily pull it out in an activity.

My question is just how to fetch instances in a non activity class.

my AppComponent

@Singleton
@Component(modules =
        {
                AndroidInjectionModule.class,
                ApplicationModule.class,
                ActivityBindingModule.class,
                RepositoryModule.class})
public interface AppComponent extends AndroidInjector<ChallengerHuntApplication> {

    RepositoryManager exposeRepositoryManager();


    @Component.Builder
    interface Builder {

        @BindsInstance
        AppComponent.Builder application(Application application);

        AppComponent build();
    }
}

RepositoryModule

@Module(includes = SystemRepositoryModule.class)
public class RepositoryModule {



}

Student Repository Module

@Module
public  class StudentRepositoryModule {


    @Singleton
    @Provides
    @Local
    public StudentDataSource provideStudentLocalDataSource(Context context) {
        return new StudentLocalDataSource(context);
    }

    @Singleton
    @Provides
    @Remote
    public StudentDataSource provideStudentRemoteDataSource(Context context) {
        return new StudentRemoteDataSource();
    }

}
Muhammad Ahmed AbuTalib
  • 4,080
  • 4
  • 36
  • 59
  • 1
    parameters injection requires an explicit moduel injection through your component interface, if you can share component/modules code we can be more specific – Fabio Nov 08 '17 at 16:44
  • @Fabio done. Apologies for not doing that earlier – Muhammad Ahmed AbuTalib Nov 08 '17 at 16:55
  • 1
    Please have a look at the documentation and make sure you understand the difference between field injection and constructor injection. *Either* have `@Inject` annotated fields, or a *single* annotation on the constructor (and make the fields private). You should only be using field injection with framework types that *must* have a no-args constructor. e.g. see [Injecting constructors](https://stackoverflow.com/q/43287645/1837367) – David Medenjak Nov 08 '17 at 19:15

0 Answers0