3

I read difference between SubComponents and Component Dependencies from this docs https://google.github.io/dagger/api/latest/dagger/Component.html and read this thread too Dagger 2 subcomponents vs component dependencies

Can anyone help me for what this line means with an example .

SubComponents may use any binding defined by their ancestor component and subcomponents. as an alternative, components can use bindings only from another component interface by declaring a component dependency. When a type is used as a component dependency, each provision method on the dependency is bound as a provider. Note that only the bindings exposed as provision methods are available through component dependencies.

N Sharma
  • 33,489
  • 95
  • 256
  • 444

1 Answers1

10

Think of it as a subcomponent like java SubComponent extends ParentComponent where everything is protected. It will have access to the same members as the parent and can just use all of the parents things.

A component dependency on the other hand is like a delegate object, where you only have access to the public methods that the class exposes.


Lets just assume we have 2 classes that Dagger can create and provide. Whatever this might be. It does not matter if you use constructor injection or create the objects from a module...We just assume we have those 2 classes that can be provided by some BaseComponent, which I will show next.

// some module can create either object
class ComponentPrivateObject {}
class ExposedObject {}

It does not matter where they come from. The next part is the interesting bit. We declare one of those classes as a return type on a method on our component interface. This is what Dagger calls a Provision Method. It says that this component can provide that class.

@Component(modules=/* some modules */)
class BaseComponent {

  // BaseComponent can provide ExposedObject!
  ExposedObject exposedObject();

}

The difference between subcomponent and dependent component is that, as with the java sample in the beginning, Subcomponent will have access to both ComponentPrivateObject and ExposedObject because it just extends the parents graph.

// can use either class, it just adds to the parent
@Subcomponent
class Subcomponent {}

On the other hand, DependentComponent only has access to ExposedObject. It does not extend the graph of BaseComponent, but it just binds BaseComponent as a provider for some objects. Like with the java sample above, its like using its public API, and if you try to use ComponentPrivateObject Dagger will tell you that it cannot be provided...

// binds component dependency as provider
@Component(dependency=BaseComponent.class)
class DependentComponent{}

One extends the parent, the other one uses its public API.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
  • 10
    Please don't "Think of it as a subcomponent like java SubComponent extends ParentComponent". There is no inheritance relationship between components and subcomponents. Rather, think of them like inner classes (they are implemented that way) in that you get access to everything from the enclosing component. – gk5885 Sep 05 '17 at 03:23
  • Aside from the previous comment, this is a great explanation! – VIN Apr 27 '22 at 15:52