Group Group Group Group Group Group Group Group Group

MVVM on Android · Model Repository | raywenderlich.com

#1

This is a companion discussion topic for the original entry at https://www.raywenderlich.com/8984-mvvm-on-android/lessons/4
#2

Hi

In the CreaturemonApplication we have the following code:

    companion object {
        lateinit var database: CreatureDatabase
    }

That means that the database dependency will be static available for the hole app. Once we have it on the Application class.

My question is: that is the best approach for sharing dependencies ?

#3

Thanks for the question, Amadeu!

The best approach is probably to use dependency injection to give the repository access to the database and the DAOs. We’re going to take a look at dependency injection in a future course. Both with a framework like Dagger and also without, e.g. you can pass the database into the repository constructor as a property.

1 Like
#4

In the the Creature class, I had to change val to var in order to run the app.

 error: Cannot find setter for field.
private final com.raywenderlich.android.creaturemon.model.CreatureAttributes attributes = null;
                                                                             ^

Otherwise I got the above error.

#5

@aaldous Thank you for sharing your solution - much appreciated! :]