Reputation: 210
This is mostly a design question. I have 2 entities Payment
and User
, having a one-to-one relationship. The API call returns a list of Payments
each Payment
contains a User
. The payments are displayed in a Recycler View, every row contains information from both the Payment
and User
entities. And to fetch them from the DB (using Room) I need to perform a join query that returns a combination of both entities:
@Query("SELECT payment.*, user.* FROM payment INNER JOIN user ON payment.user_id = user.userId")
fun findPaymentsAndUsers(): List<PaymentAndUser>
data class PaymentAndUser(
@Embedded val payment: Payment,
@Embedded val user: User)
My question is how can I create my repositories without breaking any of the Clean Architecture principles?
PaymentRepository
and handle everything from there.Every single Android Clean Architecture sample I found has one or two entities that are totally independent with no join queries or any sort of grouping, which is totally unrealistic in real life applications.
Is there any way this can be implemented correctly?
Upvotes: 3
Views: 1268
Reputation: 3573
As to my understanding SRP does NOT say that each repository should only handle its own entity - instead each repository should only have "a single reason to be changed".
The pattern that should solve your problem is the "unit of work" pattern: just create a "unit of work" repository which exactly handles this query.
In fact in his book "clean architecture" uncle blob states that repository interfaces should be designed in a way being convenient to its user. It should have one method "per query" the use case would raise against the repository.
Upvotes: 2