Koin: Provide a Room DAO ?

Created on 28 Feb 2018  路  2Comments  路  Source: InsertKoinIO/koin

i'm not sure how to provide a Room DAO, since it's an abstract class, any clues ?

Most helpful comment

This is a Room DAO provide example
Your RoomDatabase:

abstract class MyDatabase : RoomDatabase() {
    abstract fun getDao(): MyDao

companion object {
        fun buildDatabase(context: Context) =
            Room.databaseBuilder(context, MyDatabase::class.java, "MyDatabase")
                .fallbackToDestructiveMigration()
                .build()
    }
}

In your applicationContext module:

provide { MyDatabase.buildDatabase(androidApplication()).getDao() }

If the Database has more than one DAO:

provide { MyDatabase.buildDatabase(androidApplication()) }
provide { get<MyDatabase>().getDao1() }
provide { get<MyDatabase>().getDao2() }

All 2 comments

This is a Room DAO provide example
Your RoomDatabase:

abstract class MyDatabase : RoomDatabase() {
    abstract fun getDao(): MyDao

companion object {
        fun buildDatabase(context: Context) =
            Room.databaseBuilder(context, MyDatabase::class.java, "MyDatabase")
                .fallbackToDestructiveMigration()
                .build()
    }
}

In your applicationContext module:

provide { MyDatabase.buildDatabase(androidApplication()).getDao() }

If the Database has more than one DAO:

provide { MyDatabase.buildDatabase(androidApplication()) }
provide { get<MyDatabase>().getDao1() }
provide { get<MyDatabase>().getDao2() }

ah i see, almost had it,
thank you !!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

luna-vulpo picture luna-vulpo  路  4Comments

dakuenjery picture dakuenjery  路  4Comments

LukasAnda picture LukasAnda  路  3Comments

erikhuizinga picture erikhuizinga  路  3Comments

pchmielowski picture pchmielowski  路  3Comments