Is it okay to store properties in a Repository for reuse?

Viewed 325

I am making an app with some friends, and we have decided to go with the MVVM pattern. However, their understanding of the pattern differs from mine.

My question is: If we have a data that we would like to reuse in other views, can we store them as properties in a repository (seeing as the repository pattern is a singleton) and access them from other viewmodels?

Here's a generic example of what I mean:

object AnimalRepository {
       val favoriteBreed : Breed? = null
}

and we would access it like so:

class DogViewModel(
    application: Application
) : AndroidViewModel(application){

    val animalRepository = AnimalRepository 

fun setFavoriteBreed(favBreed: Breed) {
    animalRepository.favoriteBreed = favBreed
}

fun getFavoriteBreed() : Breed {
    return animalRepository.favoriteBreed
}

In this case, i did not make use of LiveData for simplicity purposes.

The debate arose from our different interpretations of this section of Android's guide to app architecture: https://developer.android.com/jetpack/docs/guide#truth

1 Answers

This is how personally I would use a Repository for and also it is how the repository can be used. Repository is the place where we have data coming from. So that makes it easy for any view or the activity to access the data from directly the Repository and can be used from any ViewModel. Does this answer your question or you need more details?

Related