3

How and where do you create the repository object in your android apps?

Do you implement your repository as a singletone? Is it a static class?

I am trying to use the single activity approach:

App structure

Apparently I wanna use the same repository in each ViewModel. But what approach do you chose or which one makes more sense to have your repository existing just once and having acces to it from everywhere?

In the android developer examples they create the repository object in the mainactivity because they need the application object to create the repository. But in this example you cannot acces it from everywhere and you could create multiple for the same reason, e.g. handling SQL.

Peter
  • 579
  • 2
  • 7
  • 18

1 Answers1

0

Did you read this guide, Guide to app architecture? I think this would answer most of your questions.

Actually you shouldn't access repository from everywhere, but from the ViewModels or even better from UseCases (Domain layer, have a look on Clean Architecture).

ViewModel delegates the data-fetching process to a new module, a repository. Repository modules handle data operations. They provide a clean API so that the rest of the app can retrieve this data easily. They know where to get the data from and what API calls to make when data is updated. You can consider repositories to be mediators between different data sources, such as persistent models, web services, and caches.

and to manage dependencies between components you can use some DI libraries.

gts13
  • 1,048
  • 1
  • 16
  • 29
  • I know this guide but it is in Kotlin which I don't understand. Additionally it's too complex if you look at the code on Github. Perhaps I was a little bit imprecisly. By "everywhere" I meant every ViewModel. Let's take a look on the codelab to room: https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#0. They create a repository instance in the ViewModel: https://github.com/googlecodelabs/android-room-with-a-view/blob/master/app/src/main/java/com/example/android/roomwordssample/WordViewModel.java. – Peter May 25 '19 at 08:41
  • 1
    But that means they could have many ViewModels and each with an own repository instance leading to inconsistent data. Am I wrong? So shouldn't the repository be a singletone then? So all ViewModels using the same repository instance? – Peter May 25 '19 at 08:43
  • The guide is in Kotlin, but the point is to get the architecture there. Each ViewModel provides data to a specific UI component, let's say for one screen. You have a screen that manages your profile, then you have a ProfileViewModel. You have another screen that manages subscriptions, then SubscriptionViewModel, and so on... This is the separation of concerns. And if you have a well structured app then you can use the same repository for all your viewmodels, and you will avoid any data inconsistency. And yes you can make it singleton – gts13 May 25 '19 at 09:48
  • 1
    this is a decent and light project https://github.com/PierceZ/GoogleArchitectureExample in java based on MVVM. Have a look – gts13 May 25 '19 at 09:51
  • Well, in this project ZooRepository is static meaning accesible from everywhere. You said: "Actually you shouldn't access repository from everywhere, but from the ViewModels or even better from UseCases" – Peter May 25 '19 at 11:16
  • yes you are right, although it is again called from viewmodels. I didn't notice the static - it's not my repo. I am working on two samples based on clean mvvm (as much as possible) but both are in Kotlin. In any case if you are interested in here is the link https://github.com/gs-ts – gts13 May 25 '19 at 11:23