Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SubComponents vs Component dependencies

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.

like image 220
N Sharma Avatar asked Jul 17 '26 00:07

N Sharma


1 Answers

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.

like image 149
David Medenjak Avatar answered Jul 18 '26 13:07

David Medenjak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!