title: Dagger 2 Injecting Constructors
body: <p>I'm starting to use Dagger 2 in an application I'm developing but I have some questions about how Dagger 2 works.</p>
  
  <p>I get the all the logic behind the @Provides methods and the @Inject annotation for initialising your dependencies, but the @Inject annotation to class constructors kind of bugs my mind.</p>
  
  <p>For example:</p>
  
  <p>Im my app, I have one module defined, the ContextModule, to retrieve the context of my application:</p>
  
  <p>ContextModule.java</p>
  
  <pre><code>@Module
  public class ContextModule {
  
      private final Context context;
  
      public ContextModule(Context context) {
          this.context = context;
      }
  
      @Provides
      public Context context() {
          return this.context;
      }
  }
  </code></pre>
  
  <p>This module is used by my BaseActivityComponent:</p>
  
  <p>BaseActivityComponent.java</p>
  
  <pre><code>@BaseActivityScope
  @Component(modules = ContextModule.class)
  public interface BaseActivityComponent {
      void injectBaseActivity(BaseActivity baseActivity);
  }
  </code></pre>
  
  <p>So far so good.. then I have an AuthController class, that depends on the context and I want to inject it in my BaseActivity. So in my AuthControllers.class I have something like:</p>
  
  <pre><code>public class AuthController {
  
      private Context context;
  
      @Inject
      public AuthController(Context context) {
          this.context = context;
      }
  
      public void auth() {
          // DO STUFF WITH CONTEXT
      }
  }
  </code></pre>
  
  <p>And I inject it in my BaseActivity like:</p>
  
  <pre><code>public class BaseActivity extends AppCompatActivity {
  
      @Inject
      AuthController authController;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          BaseActivityComponent component = DaggerBaseActivityComponent.builder()
              .contextModule(new ContextModule(this))
              .build();
  
          component.injectBaseActivity(this);
  
          authController.auth();
  
      }
  }
  </code></pre>
  
  <p>Now my question is, how does dagger knows that my AuthControllers is a dependency for BaseActivity? Just by declaring</p>
  
  <pre><code>@Inject
  AuthController authController;
  </code></pre>
  
  <p>it's like the same thing as if I created a ControllerModule like:</p>
  
  <pre><code>@Module(includes = ContextModule.class)
  public class ControllerModule {
  
      @Provides
      AuthController authController(Context context) {
          return new AuthController(context);
      }
  
  }
  </code></pre>
  
  <p>And then in my BaseActivityComponent I would add my AuthController getter and change my dependency module to ControllersModule:</p>
  
  <pre><code>@BaseActivityScope
  @Component(modules = ControllersModule.class)
  public interface BaseActivityComponent {
  
      void injectBaseActivity(BaseActivity baseActivity);
  
      AuthController getAuthController();
  }
  </code></pre>
  
  <p>When I call injectBaseActivity(this) it "tells" dagger that all @Inject annotations are dependencies of my class, and then it searchers my project for @Inject annotated constructors that matches that type?</p>
  
  <p>I thought a good thing about Dagger 2 is that the Module files could be used as a "documentation" of my dependencies three. But if just add @Inject in all the constructors I have control of, couldn't it get a little confusing in the future, since you don't know what actually depends on what? (I mean, you know what depends on what, you just have to browse a lot of files to really find out)</p>
  
  <p>Is there any best practices for when using @Inject annotations in constructors or when to add the @Provides method in Modules files?
  I get that using @Inject in constructor I don't need to change the constructor definition in my Module file, but is there any downside?</p>
  
  <p>Thanks.</p>