DEV Community

Cover image for A production-level architecture for Android apps (part 2)

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

Agustín Tomas Larghi on February 15, 2020

Previous A production-level architecture for Android apps (part 1) Agustín Tomas Larghi ・ F...
Collapse
 
zhuinden profile image
Gabor Varadi

Hey!

private fun observeQueryChanges() {
    viewModel.searchParams.observe(this, Observer {
        viewModel.searchPosts(it)
    })
}

This can actually be moved into Transformations.switchMap(searchParams) { searchPosts(it) } inside the ViewModel. That way you don't need to observe the changes here, you can just observe the posts themselves.

Collapse
 
4gus71n profile image
Agustín Tomas Larghi

Thanks for the feedback! Totally, there are a few things that I could improve on this architecture, I need to update a few things here though, for example, I've changed a bit the API of the Interactors so I can actually dispose them when the ViewModel gets destroyed.

Please let me know if you see anything else that you think could be improved. 🤓💪

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

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.
Collapse
 
4gus71n profile image
Agustín Tomas Larghi

Anyone, please feel to drop feedback/questions. I'm always trying to improve in terms of architecture design. 🤓🏗️💪