Skip to content Skip to sidebar Skip to footer

Multi-layer / Libraries Architecture With Dagger2: Designing Scopes, Components, Modules

I'm using dagger 2 in a multi-layered, multi-library Android SDK project. I'm developing many different libraries on different layers (data, business logic, presentation etc...) an

Solution 1:

I'm sorry but I don't think this idea will work because the requirements are a little bit contradictory.

Namely:

I can't provide modules only and not components (as recommended in the thread on the Dagger 2 issue tracker)

and

My most important requirement is that each library of the architecture should be usable stand-alone (with its dependent components) and that a developer should decide to build on top on any layer he (or she) wants

will be extremely difficult to engineer together. Dagger 2 uses static code generation at compile time. Components need to know about their sub-components or, alternatively, dependent components need to know about their parents. To have a turnkey Dagger 2 solution where all the consumer does is call inject from a component, you will need to setup the object graph at least partially. This is going to make it difficult to achieve the modularity you require.

Hence, I think the advice on the issue tracker thread (provide modules only and let the consumer set up Components) is the correct solution. You can use your own scope annotations and/or the JSR-330 @Singleton etc. annotations to provide hints to how the consumer of your library should set up their object graph.

Lastly, Amir Ziarati recently posted an example of an Android project with multiple modules using Dagger 2. I hope it helps, the link is here

Post a Comment for "Multi-layer / Libraries Architecture With Dagger2: Designing Scopes, Components, Modules"