How do repositories depends on use cases in clean architecture

5.2k views Asked by At

I understand how usecases should depend on entities, but I don't know how repositories depend on usecases. Say we want to store newly created users using userUsecase.register() to database, the usecase should know about the respective repository right? At least usecases know about the repository interface, not the implementation.

1

There are 1 answers

1
Andreas Hütter On BEST ANSWER

Yes, the Use Case itself should only have a dependency to the repository interface and thus you should inject the repository interface into the use case. The repository interface should be defined in the application core layer (or domain layer).

The implementation of the repository interface will reside in the infrastructure layer (also referred to as persistence layer or data layer).

So that means the repository implementation does not depend on the use case. Both, the use case and repository implementation depend only on the repository interface instead. With this the Dependency Inversion principle is applied.

With that, all dependencies are pointing inwards which makes both the use cases and the repository implementation (infrastructure) dependent on the core layer. But the core (i.e. domain) layer does not have any dependencies to outer layers.