Hilt注入子类作为父类型



我有3个存储库:

interface MainRepository {
...
}
interface LocalRepository {
...
}
interface WebRepository {
...
}

每个存储库都有自己的实现:

@Singleton
class MainRepositoryImpl @Inject constructor(
private val localRepository: LocalRepository,
private val webRepository: WebRepository
) : MainRepository {
...
}
@Singleton
class LocalRepositoryImpl @Inject constructor(
private val localMapper: LocalMapper
private val popularMovieDao: PopularMovieDao
) : LocalRepository {
...
}
@Singleton
class WebRepositoryImpl @Inject constructor(
private val webMapper: WebMapper,
private val popularMovieApi: PopularMovieApi
) : WebRepository {
...
}

正如您所看到的,MainRepository需要将其他两个存储库都注入其中,然而,我真的不知道如何做到这一点

当然,我可以用LocalRepositoryImplWebRepositoryImpl类型注入它,但我想用LocalRepositoryWebRepository类型注入它以获得更广义的方法。

这是我尝试编写的模块:

@InstallIn(ApplicationComponent::class)
@Module
object Module {
@Singleton
@Provides
fun provideWebRepository(): WebRepository {
return WebRepositoryImpl(mapper = WebMapper(), popularMovieApi = PopularMovieApi.getInstance())
}
@Singleton
@Provides
fun provideLocalRepository(): LocalRepository {
return LocalRepositoryImpl(mapper = LocalMapper(), // Here I can't really 
// figure out how to get @Dao since it requires DB 
// which requires context and etc 
// which makes me think that I've got completely wrong approach to this)
}
}

我的LocalData模块:

@InstallIn(ApplicationComponent::class)
@Module
object LocalDataSourceModule {
@Singleton
@Provides
fun provideMainDatabase(@ApplicationContext context: Context): MainDatabase = MainDatabase.getInstance(context)
@Provides
fun providePopularMovieDao(mainDatabase: MainDatabase): PopularMovieDao = mainDatabase.popularMovieDao()
}

我的WebData模块:

@InstallIn(ApplicationComponent::class)
@Module
object RemoteDataSourceModule {
@Singleton
@Provides
fun providePopularMovieApi(): PopularMovieApi = PopularMovieApi.getInstance()
}

如何在维护接口类型(LocalRepository&`WebRepository(的同时正确地注入我拥有的实现(LocalRepositoryImpl&WebRepositoryImpl(??

使用@Binds。使用以下模块代替object Module

@InstallIn(ApplicationComponent::class)
@Module
interface Module {
@Binds
fun bindWebRepository(repository: WebRepositoryImpl): WebRepository
@Binds
fun bindLocalRepository(repository: LocalRepositoryImpl): LocalRepository
}

它告诉Dagger,如果您需要WebRepository依赖关系,那么它必须提供WebRepositoryImpl,并且为LocalRepositoryLocalRepositoryImpl提供相同的依赖关系。

@Binds vs@Dagger2中提供注释的用例是什么

您的存储库

interface MainRepository {
...
}
interface LocalRepository {
...
}
interface WebRepository {
...
}

实现(此处没有@Inject或@Singleton!(

class MainRepositoryImpl constructor(
private val localRepository: LocalRepository,
private val webRepository: WebRepository
) : MainRepository {
...
}

class LocalRepositoryImpl constructor(
private val localMapper: LocalMapper
private val popularMovieDao: PopularMovieDao
) : LocalRepository {
...
}

class WebRepositoryImpl constructor(
private val webMapper: WebMapper,
private val popularMovieApi: PopularMovieApi
) : WebRepository {
...
}

Di.模块(存储库模块(

@Module
@InstallIn(ApplicationComponent::class)
object RepositoryModule {

@Singleton
@Provides
fun provideMainRepository(
localRepository: LocalRepository,
webRepository: WebRepository
): MainRepository = MainRepositoryImpl(localRepository, webRepository)
@Singleton
@Provides
fun provideLocalRepository(
localMapper: LocalMapper,
popularMovieDao: PopularMovieDao
): LocalRepository = LocalRepositoryImpl(localMapper, popularMovieDao)
@Singleton
@Provides
fun provideWebRepository(
webMapper: WebMapper,
popularMovieApi: PopularMovieApi
): WebRepository = WebRepositoryImpl(webMapper, popularMovieApi)

试试这个,告诉我它是否有效。由于您已经为所有存储库提供了@Provides,Dagger-Hilt知道如何创建它们。您的本地数据库是否使用Room?如果是,那么像您那样创建数据库可能是不对的。如果你没有使用Room,你应该开始使用,因为它会让你的生活更轻松。

以下是用dagger柄创建房间数据库的正确方法:

实体模块

@Entity(tableName = "exampleTableName")
data class ExampleEntity(
@PrimaryKey(autoGenerate = true)
val id: Int,
// ... whatever you need
val header: String = "",
val title: String = "",
val description: String = "",
)

示例数据库

@Database(entities = [ExampleEntity::class], version = 1)
abstract class ExampleDatabase : RoomDatabase() {
abstract fun exampleDao(): ExampleDao
companion object {
const val DATABASE_NAME = "example_db"
}
}

DAO

@Dao
interface DocumentDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(exampleEntity: List<ExampleEntity>)
@Query("SELECT * FROM exampleTableName")
suspend fun getList(): List<ExampleEntity>
}

房间Di.模块

@Module
@InstallIn(ApplicationComponent::class)
object RoomModule {
@Singleton
@Provides
fun provideExampleDB(@ApplicationContext context: Context): ExampleDatabase = Room.databaseBuilder(
context,
ExampleDatabase::class.java,
ExampleDatabase.DATABASE_NAME,
).fallbackToDestructiveMigration().build()
@Singleton
@Provides
fun provideExampleDAO(exampleDatabase: ExampleDatabase): ExampleDao = exampleDatabase.exampleDao()
}

最新更新