title: Dagger2: Injecting implementation classes provided by modules with component itself
body: <p>Considering modules are all shared among one-another through the Dagger1 specification of <code>complete=false, library=true</code>, you can receive the elements provided by the <code>@Provides</code> methods through a constructor parameter, like so.</p>
  
  <pre><code>public class GetUserForUsernameTaskImpl
          implements GetUserForUsernameTask {
      public static final String TAG = GetUserForUsernameTaskImpl.class.getSimpleName();
  
      private Realm realm;
      private UserRepository userRepository;
  
      public GetUserForUsernameTaskImpl(Realm realm, UserRepository userRepository) {
          this.realm = realm;
          this.userRepository = userRepository;
      }
  
      @Override
      public RealmResults&lt;UserRLM&gt; getUsers() {
          try {
              RealmResults&lt;UserRLM&gt; users = userRepository.findAll(realm);
      ...
  }
  
  @Module(includes = {RepositoryModule.class, RealmModule.class})
  public class DatabaseTaskModule {
      @Provides
      public GetUsersDatabaseTask getUsersDatabaseTask(Realm realm, UserRepository userRepository) {
          return new GetUsersDatabaseTaskImpl(realm, userRepository);
      }
  }
  </code></pre>
  
  <p>However, you could also specify only one dependency (the <code>Presenter</code> or the <code>CustomApplication</code> instance) that holds the component graph, and use that component graph to inject your implementation classes.</p>
  
  <pre><code>public class GetUserForUsernameTaskImpl
          implements GetUserForUsernameTask {
      public static final String TAG = GetUserForUsernameTaskImpl.class.getSimpleName();
  
      @Inject
      public Realm realm;
      @Inject
      public UserRepository userRepository;
  
      protected Presenter presenter;
      private boolean isInjected = false;
  
      public GetUserForUsernameTaskImpl(Presenter presenter) {
          this.presenter = presenter;
      }
  
      @Override
      public RealmResults&lt;UserRLM&gt; getUsers() {
          if(!isInjected) {
              presenter.getPresenterComponent().inject(this);
              isInjected = true;
          }
          try {
              RealmResults&lt;UserRLM&gt; users = userRepository.findAll(realm);
              ...
      }
  }
  
  @Module(includes = {PresenterModule.class})
  public class DatabaseTaskModule {
      @Provides
      public GetUsersDatabaseTask getUsersDatabaseTask(Presenter presenter) {
          return new GetUsersDatabaseTaskImpl(presenter);
      }
  }
  </code></pre>
  
  <p>And that way, you'd only have to depend on the presenter's object graph, and not have to mess with constructor parameters.</p>
  
  <p>Which one is a better approach?</p>
  
  <p><strong>EDIT:</strong> A clearer and concrete example I have in a not-too-well refactored project is the following:</p>
  
  <pre><code>@Module(includes = {ContextModule.class})
  public class ClientAuthModule {
      @Provides
      public ClientAuthAuthenticator clientAuthAuthenticator(CustomApplication customApplication) {
          return new ClientAuthAuthenticator(customApplication);
      }
  }
  </code></pre>
  
  <p>Then</p>
  
  <pre><code>public class CustomApplication
          extends Application {
      public static class InjectorInitializedEvent {
      }
  
      public static class InjectorInitializedEventProducer {
          @Produce
          public InjectorInitializedEvent produceEvent() {
              return new InjectorInitializedEvent();
          }
      }
  
      private ApplicationComponent applicationComponent;
  
      @Override
      public void onCreate() {
          super.onCreate();
          applicationComponent = Injector.INSTANCE.initializeApplicationComponent();
          SingletonBus.INSTANCE.getBus().post(new InjectorInitializedEvent());
          SingletonBus.INSTANCE.getBus().register(new InjectorInitializedEventProducer()); //OTTO bus, event producer
      }
  
      public ApplicationComponent getApplicationComponent() {
          return this.applicationComponent;
      }
  }
  </code></pre>
  
  <p>Then</p>
  
  <pre><code>public class ClientAuthAuthenticator {
      private CustomApplication customApplication;
  
      @Inject
      public PEMConverter pemConverter;
      @Inject
      public KeyPairCreator keyPairCreator;
      @Inject
      public PKCS10CsrCreator pkcs10CsrCreator;
      @Inject
      public KeyPairReader keyPairReader;
      //...
  
      public ClientAuthAuthenticator(CustomApplication customApplication) {
          this.customApplication = customApplication;
          SingletonBus.INSTANCE.getBus().register(this);
      }
  
      @Subscribe
      public void onInjectorInitializedEvent(CustomApplication.InjectorInitializedEvent e) {
          customApplication.getApplicationComponent().inject(this);
          SingletonBus.INSTANCE.getBus().unregister(this);
      }
  
      ...
  </code></pre>
  
  <p><strong>The question:</strong> This way, all dependencies are provided from the application's component when the injector is ready, rather than through the constructor. But is this a good approach? Are there any caveats on the long-run?</p>
  
  <p><strong>EDIT2:</strong> I had the injector here, but it was pretty bad.</p>
  
  <p>The one I have <a href="https://stackoverflow.com/questions/31834056/how-do-you-organise-your-dagger-2-modules-and-components/31835913#31835913">in my other project was better constructed</a>. </p>