简体   繁体   English

Koin Android-KMM:我有嵌套范围但注入不起作用

[英]Koin Android-KMM: I have nested scopes but injection is not working

I am getting this error.我收到此错误。 I am using Koin for dependency injection.我正在使用 Koin 进行依赖注入。 I want my appointment repository to be alive as UserInfoContainer scope is alive.我希望我的约会存储库是活的,因为 UserInfoContainer scope 是活的。 No definition found for class:'com.flow.domain.repository.AppointmentsRepository'.找不到 class 的定义:'com.flow.domain.repository.AppointmentsRepository'。 Check your definitions!检查你的定义!

UserInfoContainer class用户信息容器 class

class UserInfoContainer(private val encryptedLocalDatabase: EncryptedLocalDatabase) :
    KoinScopeComponent {
    override val scope: Scope get() = getOrCreateScope().value
var user: User?
    get() = encryptedLocalDatabase.user
    set(it) {
        if (it != encryptedLocalDatabase.user) {
            encryptedLocalDatabase.user = it
            scope.close()
        }
        }
}

Koin file公因文件

single { UserInfoContainer(encryptedLocalDatabase = get()) }

    scope<UserInfoContainer> {
        scoped<AppointmentsRepository> {
            AppointmentsRepositoryImplementation(
                apiService = get(),
                clinicId = get<UserInfoContainer>().user.let { it!!.clinicId }
            )
        }
    }

AppointmentsUseCase class约会用例 class

class AppointmentsUseCase : KoinComponent {
    private val appointmentsRepository: AppointmentsRepository by inject()

    suspend fun getAppointments(startDate: LocalDateTime, endDate: LocalDateTime): List<Appointment> =
        appointmentsRepository.getAppointments(startDate, endDate)
}

That is how I should inject my dependencies in the UseCase这就是我应该如何在用例中注入我的依赖项

class AppointmentsUseCase : KoinComponent {
    private val userInfoContainer: UserInfoContainer by inject()
    private val appointmentsRepository: AppointmentsRepository = userInfoContainer.scope.get()

    suspend fun getAppointments(
        startDate: LocalDateTime,
        endDate: LocalDateTime
    ): List<Appointment> =
        appointmentsRepository.getAppointments(startDate, endDate)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM