Dagger in separate gradle module
Asked Answered
T

1

7

I have 3 gradle modules in my 'clean architecture' Android application: 'data', 'domain' and 'presentation'. 'data' and 'presentation' both depend on 'domain', but not each other. 'presentation' holds Application class realization, and 'data' holds realization of some repositories singletons.

I'd like to use Dagger 2 for instantiating repositories in Application, but in this case I need to make indirect gradle dependency between 'data' and 'presentation'. Such a dependency looks ugly from Clean architecture point of view, making possible access 'data' from 'presentation'. Placing Dagger components and modules code in separate gradle module 'di' creates circular gradle dependency 'data' -> 'di' -> 'data'.

Is there a proper way to move all the DI code in separate module?

Tomchay answered 26/7, 2016 at 18:27 Comment(1)
Have you found any solution to that? That's an interesting idea but I don't know how to avoid circular dependency in that case.Roca
G
0

I don't think that it's a bad practice to put dependency between 'presentation' and 'infrastructure' ('data' in your case).

You can put all the technical dependencies in the infrastructure layer and call it from application layer ('presentation' in your case).

This is because, in some cases, you need to call infrastructure without calling domain layer : if you don't have business rules.

The most important thing in clean architecture is to separate the domain from all the other things (espacially technical things).

You can also have application and infrastructure layers in the same module.

With these dependencies, you don't have circular dependecies :

application -> domain 
application -> infrastructure
infrastructure -> domain

you could have it if you had the following dependency :

domain -> infrastructure

That said, you shouldn't instantiate Dagger classes in application layer. All the data access instantiations must be in the infrastructure layer. You can call data access interface from application and implement it with Dagger in the infrastructure layer.

Gnathion answered 27/6, 2020 at 12:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.