Hi, I want to have my database and repository pro...
# android
j
Hi, I want to have my database and repository properties into my Application class. It's working but I wonder if this is okey and clean way of doing this ? Maybe i will use dagger in the future but not now.
Copy code
class MyApp : Application() {
    val database: AppDatabase by lazy { AppDatabase.getInstance(this) }
    val dataRepository: DataRepository by lazy { DataRepository(database.documentDao) }
}
馃憤 1
b
I think this is a great way to expose singleton application scope without adding in a framework.
If the dependency is application scoped might be worth throwing it in MyApp companion object for ergonomics
j
Thanks for the advice 馃檪, cleaner than before
But for the database I use
this
, how can I pass application instance (for context) in the companion object ?
b
Copy code
class App : Application {
  companion object {
    lateinit var appInstance: App

    val dateabase: AppDatabase by lazy { AppDatabase.getInstance(appInstance) }
  }

  onCreate {
    appInstance = this
  }
}
j
Thanks 馃檪
馃憤 1