android10 / Android-CleanArchitecture

This is a sample app that is part of a series of blog posts I have written about how to architect an android application using Uncle Bob's clean architecture approach.
Apache License 2.0
15.52k stars 3.32k forks source link

Why do you do this? #272

Open NerdAnonymous opened 6 years ago

NerdAnonymous commented 6 years ago

why UserRepository interface is in the domain layer?Does this cause the data layer to rely on the domain layer?right?

alexwhb commented 6 years ago

@NerdAnonymous It's fine for the data layer to depend on the domain layer, it is not fine for the domain layer to depend on any other layer. If you did not put the interfaces in the domain layer, in order to use the data layer repositories, you'd need to have the domain layer depend on the data layer. But because this project uses dependency injection the domain layer does not need to know anything about the data layer as long as those classes implement the domain layer interfaces. I hope that makes sense.

NerdAnonymous commented 6 years ago

@alexwhb I seem to have a little understanding of what you say. But there are some questions. In the case, data layer is Entities, domain layer is Use Cases, according to Clean Architecture Dependency. It should be that domain layer depends on data layer, but sample is just the opposite, and I don't understand why. Is it just because of the dependence on injection?

kevin-barrientos commented 6 years ago

@NerdAnonymous no, you're confused. Domain must not depend on any other layer. It is the most inner layer and therefore the most abstract. If you need to fetch some data, you abstract that service with an interface (i.e. a connector) and implemented somewhere else like the data layer.

There are two kinds of dependencies:

  1. Compile time dependencies
  2. Runtime dependencies

The arquitecture prevent the domain layer to depend on any other layer in compile time but in runtime the domain layer would depend on the implementation of the interface (i.e. the data layer) which is fine. This is the so called IoC (inversion of control).

NerdAnonymous commented 6 years ago

@kevin-barrientos Well, This seems to be the emphasis on the Dependence Inversion Principle principle.But I still not fully understand,so I must work hard at it