Hey all ! I was wondering how do you structure you...
# android-architecture
m
Hey all ! I was wondering how do you structure your project to apply dependency inversion between modules - it seems to end up in low modules depending on high ones for sake of inversion - but I was wondering if this is correct approach in general, or something else ?
e
Following clean architecture principles the inner most module (domain) doesn't depend in any other modules. External modules (network, database, etc) may depend only in modules closer to the center than them. For example:
network
may depend on
domain
but no on
database
i
But following the same architecture, your project structure should expose what it do, not what architecture it uses. So, packaging by feature instead of layer makes sense too.
The downside of modularizing by feature is not enforcing layer visibility
t
I guess it is easy to use this way, the inner module creates interfaces, the outer module implements the interface, and the implementation is injected into inner module as the interface type so it still does not depend on any outer module.
m
CA is what I have right now, but considering DI description these solutions don’t seem complete. Following example above, to apply inversion
domain
should depend on
network
and
database
so it knows what abstractions they need, therefore
network
and
database
don’t depend on anything concrete from
domain
.
t
No, domain is not depending on outer module, I think this is the main idea behind dependency inversion. A purpose of CA is to abstract app specific implementation from anything library/framework and I think it is achieved this way.
m
Inversion says higher level modules should not depend on lower level modules and vice-versa. They should both depend on abstractions.
depend
doesn’t mean gradle dependency here - unlike my question, but it goes that way if I’m to separate
network
from
domain
.
What I’m asking is not how to apply but rather is there another way to do it without making modules depending on each other - in gradle way. Like
domain
depends on
network
to know what abstractions it works with - not to fulfill it’s own rules. I was wondering maybe are some other practices like - just as a thought - creating a middle layer for it.
t
Maybe the
ports
-like layer in hexagonal architecture?
m
Sounds interesting ! I’ll check it out 💃
e
But
domain
shouldn't depend on
network
.
Domain
defines
interfaces
and it is the
network
job to comply with said interfaces. Doesn't matter what abstractions
network
uses. For example, in domain we define an interface
Copy code
interface NetworkRepositoy {
    suspend fun getUser() : User //user is the domain entity, no depency with network here
}
Then in the network module this interface is implemented. This implementation may depend on external libraries like ktor or retrofit, and will probably map network models to the domain models. But there is no gradle dependency of
domain
to
network
, only from
network
to
domain
I don't know if I am missing something or don't understand the problem properly
j
+1 to what @Eric Martori says.