-2

In my android application am using MVVM architecture and using koin library for DI. Below is my Repository class:

class JaiminRepository constructor(
    private var remoteDataSource : RemoteDataSource,
    private var enrollApiInterface : EnrollApiInterface
) {
   ...
  }

Created module for is as below:

val jaiminRepositoryModule = module {

    single {
        JaiminRepository(get(),get())
    }

}

For this I am getting error as :

Instance creation error : could not create instance for [Singleton:'com.jaimin.sdk.repository.JaiminRepository']: org.koin.core.error.NoBeanDefFoundException: |- No definition found for class:'com.jaimin.api.RemoteDataSource'. Check your definitions! org.koin.core.scope.Scope.throwDefinitionNotFound(Scope.kt:287) org.koin.core.scope.Scope.resolveValue(Scope.kt:257)

So I have added factory for RemoteDataSource.

factory {
    RemoteDataSource()
}

and finally it look like as below:

val jaiminRepositoryModule = module {

        factory {
            RemoteDataSource()
        }

        single {
            JaiminRepository(get(),get())
        }

    }

But still am getting error. What might be the issue? Do I need to do something with EnrollApiInterface also? Please guide. Thanks in Advance.

Stephan
  • 15,704
  • 7
  • 48
  • 63
Jaimin Modi
  • 1,530
  • 4
  • 20
  • 72

1 Answers1

2

You have to define all dependencies in the module(or another module), otherwise your repository can't be created. Make sure you also provide the EnrollApiInterface:

val jaiminRepositoryModule = module {

    factory<EnrollApiInterface> {
        EnrollApiInterfaceImpl()
    }

    factory {
        RemoteDataSource()
    }

    single {
        JaiminRepository(get(),get())
    }

}
Stephan
  • 15,704
  • 7
  • 48
  • 63
  • EnrollApiInterface is an interface. While writing factory for it, displays "Required EnrollApiInterface, found Unit" – Jaimin Modi Feb 13 '23 at 09:18
  • It doesn't matter if it's an interface or a class, you have to provide it so koin can find it. How you provide it is up to you, the code I provided is just an example. You can also use `object: EnrollApiInterface {…` instead of EnrollApiInterfaceImpl in the factory. – Stephan Feb 13 '23 at 09:28