Dagger 2 subcomponents vs component dependencies
Here is the code example with screenshot for more understanding of Component and SubComponent:
Component:
- AppComponent contains two declarations.
- AppComponent initializes into App class.
- HomeActivityComponent is dependent upon AppComponent.
- In HomeActivity on initialization of DaggerHomeActivityComponent, I am giving AppComponent object as a composition.
SubComponent:
- AppComponent contains SubComponent or SubComponents.
- AppComponent initializes into App class.
- SubComponent doesn’t know about his ParentComponent. That only providing its own dependencies by including Module.
- In HomeActivity I am injecting SubComponent by using its Parent Component.
And the Pictorial Diagram:
Source: link
According to the documentation:
Component Dependency
gives you access to only the bindings exposed as provision methods through component dependencies, i.e. you have access to only types which are declared in parent Component
.
SubComponent
gives you an access to the entire binding graph from its parent when it is declared, i.e. you have an access to all objects declared in its Module
s.
Let's say, you have an ApplicationComponent
containing all Android
related stuff (LocationService
, Resources
, SharedPreference
, etc). You also want to have your DataComponent
where you manage things for persistence along with WebService
to deal with APIs. The only thing you lack in DataComponent
is Application Context
which resides in ApplicationComponent
. The simplest way to get a Context
from DataComponent
would be a dependency on ApplicationComponent
. You need to be sure you have a Context
explicitly declared in ApplicationComponent
because you only have access to declared stuff. In this case, there is no manual work, meaning you don't need to specify Submodules
in parent Component
and explicitly add your submodule to a parent module like:
MySubcomponent mySubcomponent = myComponent.plus(new ChildGraphModule("child!")); // No need!
Now consider that case where you want to inject WebService
from DataComponent
and LocationService
from ApplicationComponent
into your Fragment
which binds using the @Submodule
plus
feature above. The cool thing here is that the component you're binding to (ApplicationComponent
) does not need to expose WebService
nor LocationService
because you have access to the entire graph right away.
Component dependencies - Use this when you want to keep two components independent.
Subcomponents - Use this when you want to keep two components coupled.
I will use the below example to explain Component dependencies and Subcomponents. Some points worth noticing about the example are:
SomeClassA1
can be created without any dependency.ModuleA
provides and instance ofSomeClassA1
via theprovideSomeClassA1()
method.SomeClassB1
cannot be created withoutSomeClassA1
.ModuleB
can provide an instance ofSomeClassB1
only if an instance ofSomeClassA1
is passed as an argument toprovideSomeClassB1()
method.
@Module
public class ModuleA {
@Provides
public SomeClassA1 provideSomeClassA1() {
return new SomeClassA1();
}
}
@Module
public class ModuleB {
@Provides
public SomeClassB1 provideSomeClassB1(SomeClassA1 someClassA1) {
return new SomeClassB1(someClassA1);
}
}
public class SomeClassA1 {
public SomeClassA1() {}
}
public class SomeClassB1 {
private SomeClassA1 someClassA1;
public SomeClassB1(SomeClassA1 someClassA1) {
this.someClassA1 = someClassA1;
}
}
Dagger will take care of passing the instance of SomeClassA1
as an argument to provideSomeClassB1()
method on ModuleB
whenever the Component/Subcomponent declaring ModuleB
is initialized. We need to instruct Dagger how to fulfill the dependency. This can be done either by using Component dependency or Subcomponent.
Component dependency
Note the following points in the Component dependency example below:
ComponentB
has to define the dependency via thedependencies
method on@Component
annotation.ComponentA
doesn't need to declareModuleB
. This keeps the two components independent.
public class ComponentDependency {
@Component(modules = ModuleA.class)
public interface ComponentA {
SomeClassA1 someClassA1();
}
@Component(modules = ModuleB.class, dependencies = ComponentA.class)
public interface ComponentB {
SomeClassB1 someClassB1();
}
public static void main(String[] args) {
ModuleA moduleA = new ModuleA();
ComponentA componentA = DaggerComponentDependency_ComponentA.builder()
.moduleA(moduleA)
.build();
ModuleB moduleB = new ModuleB();
ComponentB componentB = DaggerComponentDependency_ComponentB.builder()
.moduleB(moduleB)
.componentA(componentA)
.build();
}
}
SubComponent
Note the following points in the SubComponent example:
- As
ComponentB
has not defined the dependency onModuleA
, it cannot live independently. It becomes dependent on the component that will provide theModuleA
. Hence it has a@Subcomponent
annotation. ComponentA
has declaredModuleB
via the interface methodcomponentB()
. This makes the two components coupled. In fact,ComponentB
can only be initialized viaComponentA
.
public class SubComponent {
@Component(modules = ModuleA.class)
public interface ComponentA {
ComponentB componentB(ModuleB moduleB);
}
@Subcomponent(modules = ModuleB.class)
public interface ComponentB {
SomeClassB1 someClassB1();
}
public static void main(String[] args) {
ModuleA moduleA = new ModuleA();
ComponentA componentA = DaggerSubComponent_ComponentA.builder()
.moduleA(moduleA)
.build();
ModuleB moduleB = new ModuleB();
ComponentB componentB = componentA.componentB(moduleB);
}
}