Dagger: Issue Providing interface With different implementation with generics

Created on 9 Jul 2017  Â·  2Comments  Â·  Source: google/dagger

Hi, i have been trying to provide different implementation
```
interface ICurrentUserRepo {

fun get(): output

fun update(input: input)

fun delete(input: input)

fun insert(input: input)

}```

and i'm providing two implementation of the interface by dagger
```@Dao
interface LocalUserRepo : ICurrentUserRepo> {

@Query("SELECT * FROM CURRENT_USER")
override fun get(): Flowable<CurrentUser>

@Delete
override fun delete(input: CurrentUser)

@Insert
override fun insert(input: CurrentUser)

@Update
override fun update(input: CurrentUser)

}```

second one
```class RemoteUserRepo(val remoteUsersDB: DatabaseReference) : ICurrentUserRepo {

override fun update(input: Params) {
    TODO("not implemented for now, should update user info in database")
}

override fun get() {
    TODO("not implemented for now, should return user from database")
}

override fun delete(input: Params) {
    TODO("not implemented for now, should delete user from database")
}

override fun insert(input: Params) {

}

open class Params(val user: CurrentUser, val observer: DisposableCompletableObserver)

}```

This my module
```@Provides
@Singleton
@LocalUserDb
internal fun provideLocalUserDb(database: UserDatabase) : ICurrentUserRepo>
= database.CurrentUserDao()

@Provides
@Singleton
@RemoteUserDb
internal fun provideRemoteUserDb(@FirebaseUsersTable usersTable: DatabaseReference): ICurrentUserRepo<RemoteUserRepo.Params, Unit>
        = RemoteUserRepo(usersTable)```

[4:24]
why i cant provide different type of interface implementation with dagger ?
i get this error
/home/darel/StudioProjects/XXX/app/build/tmp/kapt3/stubs/debug/com/xxx/xxxx/di/components/XXXAppComponent.java:6: error: [com.XXXX.XXX.di.components.AuthenticationComponent.inject(com.XXX.XXX.authentication.view.AuthActivity)] @com.XXX.XXX.di.LocalUserDb com.XXX.XXX.data.ICurrentUserRepo> cannot be provided without an @Provides-annotated method.

public abstract interface SociallyAppComponent {
e: ^
e: @com.XXX.XXX.di.LocalUserDb com.XXX.XXX.data.ICurrentUserRepo> is injected at
e: com.XXX.XXX.di.modules.AuthenticationModule.provideAuthPresenter(…, localDb, …)
e: com.XXX.XXX.authentication.presenters.AuthPresenter is injected at
e: com.XXX.XXX.authentication.view.AuthActivity.presenter
e: com.XXX.XXX.authentication.view.AuthActivity is injected at
e: com.XXX.XXX.di.components.AuthenticationComponent.inject(authActivity)

Can we do this with dagger ? or do we need to add any stuff or it's a bug ?

Most helpful comment

I think you are having wildcard/kotlin issue on generics. You need to add @JvmSuppressWildcards when injecting your ICurrentUserRepo in AuthActivity. Here's an example from Stackoverflow https://stackoverflow.com/questions/43141740/dagger-2-multibindings-with-kotlin

Here's another example:

@Inject constructor(val jobs: @JvmSuppressWildcards Map<String, Provider<JobTime>>)  

All 2 comments

I think you are having wildcard/kotlin issue on generics. You need to add @JvmSuppressWildcards when injecting your ICurrentUserRepo in AuthActivity. Here's an example from Stackoverflow https://stackoverflow.com/questions/43141740/dagger-2-multibindings-with-kotlin

Here's another example:

@Inject constructor(val jobs: @JvmSuppressWildcards Map<String, Provider<JobTime>>)  

THANKS A LOT! that was the issue...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vorburger picture vorburger  Â·  4Comments

pyricau picture pyricau  Â·  4Comments

peter-tackage picture peter-tackage  Â·  3Comments

rciovati picture rciovati  Â·  3Comments

feinstein picture feinstein  Â·  3Comments