Hello guys, how are you doing? I'd like to know i...
# koin
c
Hello guys, how are you doing? I'd like to know if there is a way to get rid of the get() method by using singleOf in a scenario where I don't reference a class constructor with :: (Because in my case it's not even a class, it is just a file with an extension function inside), but instead, there is a factory method inside of it This is my module today:
Copy code
val module = lazyModule {
    singleOf(::ChuckerInterceptor) bind Interceptor::class
    single { newInstance(interceptor = get()) }
}
And this is the dependency needing method:
Copy code
//SourceConnector.kt
fun Scope.newInstance(
    baseUrl: String = BuildConfig.BASE_URL,
    interceptor: Interceptor,
): HttpClient =
    HttpClient(OkHttp) {
       ...
       ...
    }
I tried to reference that method in different manners but none worked First I turned the file into an object and tried to reference it (Had to remove Koin's Scope requirement since AFAIK singleOf does not provide a Scope context):
Copy code
singleOf(SourceConnector::newInstance)
Then, I reverted that change and went back to having just a file and tried to reference that method:
Copy code
singleOf(::newInstance)
But I always got an exception that says Koin could not find the definition (NoBeanDefFoundException) I'd like to keep it as an extension function that does not require a prior object creation to be accessed if possible Do you think there is a way to use singleOf(::X) in this case or should I keep the code as it is? Thank you so much in advance 🤝
a
I believe it could try to resolve on your `baseUrl`argument, and cause NoBeanDefFound
👍 1
can you remove it ?
👍 1
or overload manually, having a second function that has only the arguments you want to resolve
👍 1
c
Hello Arnaud, how are you doing? Oh I see. I'll try to change it and see if it works Thanks for your response 👍
p
I'm curious with this implementation. How could you retrieve this dependency? I'm guessing something like this: val customFunction = get<??>??
c
Hello Pedro, how are you doing? Well, basically today I use get() to inject the dependency into the newInstance() factory method then encapsulate the resulting dependency inside a single {} which is then given to classes who need it This is the class (File) that I use to create an HTTP Client instance: https://github.com/caiodev/GithubProfileSearcher/blob/edge___often_broken___/modul[…]ofilesearcher/datasource/fetchers/remote/api/SourceConnector.kt And this is the class who instantiates it as a dependency https://github.com/caiodev/GithubProfileSearcher/blob/edge___often_broken___/modul[…]odev/com/br/githubprofilesearcher/datasource/di/DatasourceDI.kt At line 26 of DatasourceDI.kt, I create an instance of the dependency newINstance needs which is an OkHttp Interceptor Then, at line 27, once it's already been created, I use get() to inject it inside newInstance() which consequently, will give me the final dependency I need. A Ktor HttpClient that is I don't know if I have made myself clear but do feel free to let me know if you still have any questions and/or suggestions about how this process works koinscroll
Btw, I just tested what you suggested, @arnaud.giuliani and that's exactly it Koin tries to resolve not only the OkHttp Interceptor which I have a predefined dependency for, but also the base URL I already have in place that is obtained from BuildConfig This way, I could reference the factory method directly like this:
Copy code
singleOf(::newInstance)
It is way cleaner and concise since I will only have one URL for the whole project, this is perfect But since I need that default URL on the method's signature, would there be a way to tell Koin to skip that dependency and only inject the OkHttp Interceptor (In this specific case, but basically a way to ignore some dependencies even if it means an Annotation to do so)? Thank you so much for your tip koinscroll
👍 1
p
> At line 26 of DatasourceDI.kt, I create an instance of the dependency newINstance needs which is an OkHttp Interceptor > > Then, at line 27, once it's already been created, I use get() to inject it inside newInstance() which consequently, will give me the final dependency I need. A Ktor HttpClient that is Very smart! I never thought in use it like this. And I just noticed right now that receive from extension function was based in Scope from Koin. Now I get it!
🦜 1
🤝 1
c
Thank you so much @Pedro Francisco de Sousa Neto Glad I could help 😄 Any other questions you may have, feel free to get in touch 🤝