https://kotlinlang.org logo
Title
h

Harnick

03/30/2023, 11:45 PM
I have an AppComponent with bindings, which require a provider for a database instance. Since I'm using SQLDelight, I need to provide a unique DriverFactory instance for each platform, which I'm doing so with a child PlatformComponent, but it doesn't seem like the provider's being passed up to the parent. What is it I'm doing wrong?
[ksp] Cannot find an @Inject constructor or provider for: com.harnick.troupetent.database.PersistentStorage
@Scope
annotation class Singleton

@Singleton
@Component
abstract class AppComponent {

    @Provides
    fun ktorClient(): HttpClient = HttpClient()

    @Provides
    fun SettingsRepoImpl.bind(): SettingsRepo = this

    @Provides
    fun StatRepoImpl.bind(): StatRepo = this
}
expect abstract class PlatformComponent {
    fun createDb(): PersistentStorage
}
@Component
actual abstract class PlatformComponent(
    @Component val appComponent: AppComponent,
    @get:Provides protected val appContext: Application
) {
    @Provides
    actual fun createDb(): PersistentStorage = createDatabase(DriverFactory(appContext))
}
e

evant

03/31/2023, 12:10 AM
You can only look up for dependencies not down. This prevents memory leaks when the parent scope is larger than the child. You could invert the ordering of your components here, or make PlatformComponent and interface AppComponent implements
h

Harnick

03/31/2023, 1:53 AM
I require constructor parameters, so couldn't use an interface, but I inverted the ordering as you mentioned and it works fine. Thank you!