How does repository depends on usecase clean architecture

Viewed 3235

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

1 Answers

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.

Related