question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component which dependent from other components with different scopes

See original GitHub issue

Hello, i have complex multi-tier architecture in my Android project.

Currently i want to use the following structure of the DI components and modules:

[Data Layer]
	@DataScope //scope is used for caching (Singleton) some Data Layer entities for whole application
	- DataComponent //exposes just interfaces which should be used on the BL Layer
		//Modules exposes entities for internal (Data Layer) injections and entities which exposed by DataComponent for BL Layer
		* DataModule1
		* DataModule2
		* DataModule3

[Business Logic Layer] (also has component dependency on DataComponent)
	@BlScope //scope is used for caching (Singleton) some BL Layer entities for whole application
	- BlComponent //exposes just interfaces which should be used on the Service Layer & Presentation Layer
		//Modules exposes entities for internal (BL Layer) injections and entities which exposed by BLComponent for the Service Layer & Presentation Layer
		* BlModule1
		* BlModule2

[Service Layer] (also has component dependency on BlComponent) - this layer has Android specific entities (Android Service, ContentProvider) not related to the Presentation Layer
	@ServiceScope //scope is used for caching (Singleton) some Service Layer entities for whole application
	- ServiceComponent //exposes just interfaces which should be used on the Presentation Layer
		* ServiceModule //Module exposes entities for internal (Service Layer) injections and entities which exposed by ServiceComponent for the Presentation Layer

[Presentation Layer] (also has component dependency on: ServiceComponent, BlComponent)
	@PresentationScope //scope is used for caching (Singleton) some Presentation Layer entities for whole application
	- PresentationComponent //exposes just interfaces which should be used on the current layer
		* PresentationModule //Module exposes entities injections on the current layer

To build the main graph i use the following code:

DataComponent dataComponent = DaggerDataComponent.builder().<addModules>.build();
BlComponent dataComponent = DaggerBlComponent.builder().<addModules>.dataComponent(dataComponent).build();
ServiceComponent serviceComponent = DaggerServiceComponent.builder().<addModule>.blComponent(blComponent).build();
PresentationComponent presentationComponent = DaggerPresentationComponent.builder().<addModule>.blComponent(blComponent).serviceComponent(serviceComponent).build();

In the PresentationLayer i use only “presentationComponent” to provide required dependencies from ServiceComponent/Layer and BLComponent/Layer.

Currently scenario above doesn’t work, because PresentationComponent depends on the 2 scoped components with the error “…depends on more than one scoped component:…”. Though it allows to use one scoped component with many non-scoped components. This architecture is directed to restrict to use internal layer entities on the upper layers and in the same time to have independent tests (unit & instrumentation) on each layer/module.

Could anybody help me to understand is it bug or desirable behavior of the Dagger processor? (and why?)

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

3reactions
Tagakovcommented, Jul 17, 2018

In the #1225 @ronshapiro confirmed usage of interfaces without @Component annotation as dependencies as valid approach.

0reactions
Tagakovcommented, Jul 14, 2018

I agree with @ultraon. What is the motivation behind the decision to allow having only one scoped component’s dependency? The answer on SO explains nothing. As @ultraon mentioned on SO if component’s dependencies don’t expose the same types everything should be fine. In any case you can’t know that the type provided by provision method is scoped or not (or distinguish its scope).

In my case I’ve used interfaces only with provision methods extracted from my components as dependencies, which hides the actual components and their scopes. After @guliash showed me this issue, I began to worry that by hiding components (and scopes) behind interfaces, I was using a bug in validation and as soon as it was fixed by forbidding usage of interfaces not marked by @Component annotation as component’s dependencies, all my architecture would block me from updating dagger in project. @ronshapiro, is using interfaces without @Component annotation as dependencies of components a valid approach or is it a bug that would be fixed in future?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Component which dependent from other ... - Stack Overflow
Component which dependent from other components with different scopes (component hierarchies with different scopes).
Read more >
Dagger 2. Part II. Custom scopes, Component dependencies ...
Two dependent components cannot have the same Scope . More details here; Parent component must explicitly declare objects which can be used in ......
Read more >
Subcomponents - Dagger
Subcomponents are components that inherit and extend the object graph of a parent component. You can use them to partition your application's object...
Read more >
Component scoped dependencies - Blazor University
So far we've learned about the three dependency injection scopes: Singleton, Scoped, and Transient. We've also experimented to see how these different ......
Read more >
Dagger 2 same scope dependency - Medium
In my example of Setup Android Modules with Dagger 2, due to different module have different component, I can't have them in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found