DEV Community

loading...

Discussion on: A production-level architecture for Android apps (part 2)

Collapse
kevschweitzer profile image
kevschweitzer • Edited

I'm trying to learn from this post while i'm developing an app and i have a situation that i don't know how to solve:
I have two entities (let's say User and Address) and a user has a foreign key to an Address. When i want to save a user i need to create an Address (using AddressRepository) and save a User referencing that address id (using UserRepository). The thing is that i don't know where to put this logic because i need access to two repositories in the same place. As i understand a repository should not have a reference to other repository, but i guess it would neither be correct to do this management from UseCase/Interactor because that would expose how is database implemented. What do you think about it?

Collapse
4gus71n profile image
Agustín Tomas Larghi Author

but i guess it would neither be correct to do this management from UseCase/Interactor because that would expose how is database implemented. What do you think about it?

I don't see why it would be a bad idea having a InsertUserWithAddressUseCase which internally uses two different DB repositories.

I'm going to guess that the technology that you're using for the database layer is Room. This is a very brute, raw example of how that would look like, let me know if it fits your needs, or if you're looking for something else:

gist.github.com/4gus71n/2c0b0fe131...

A note though:

  • I guess that you actually have a more complex situation going on, but if you actually need to just insert one record that has a one-to-many relationship there are way easier ways to do it than the one above.