3

In Dagger I sometimes see that there are components that just extend an interface while others use the dependencies.

So for example we have a base component:

@Singleton
@Component(modules={...})
public interface BaseComponent {
    ...
}

Version 1:

@Singleton
@Component(modules={...})
public interface MyComponent extends BaseComponent {
    ...
}

And Version 2:

@CustomScope
@Component(modules={...}, dependencies= BaseComponent.class)
public interface MyComponent {
    ...
}

Are they used for different scenarios?

Rgfvfk Iff
  • 1,549
  • 2
  • 23
  • 47

1 Answers1

5

If you want to create a hierarchy of components, the standard ways to do this are to use subcomponents or to use dependent components.

You can use interface extension to make test components. This is a better solution than extending modules. See here for an explanation from the official docs:

@Component(modules = {
  OAuthModule.class, // real auth
  FooServiceModule.class, // real backend
  OtherApplicationModule.class,
  /* … */ })
interface ProductionComponent {
  Server server();
}

@Component(modules = {
  FakeAuthModule.class, // fake auth
  FakeFooServiceModule.class, // fake backend
  OtherApplicationModule.class,
  /* … */})
interface TestComponent extends ProductionComponent {
  FakeAuthManager fakeAuthManager();
  FakeFooService fakeFooService();
}
David Rawson
  • 20,912
  • 7
  • 88
  • 124
  • 2
    I wouldn't say those member injectors in ComponentB are necessarily undesirable. This is how you'd use ComponentB in testing. See `Option 2: Separate component configurations` in docs: https://google.github.io/dagger/testing – tir38 Oct 12 '18 at 17:47