I know there's the `get` from `KoinJavaComponent` ...
# koin
m
I know there's the
get
from
KoinJavaComponent
, but not sure if that is the preferred method for getting on demand instances in Kotlin code
a
don’t use
KoinJavaComponent
if you don’t need Java compat
m
I usually prefer injecting in the constructor, imho makes things easier to test and makes your class independent of Koin. Not sure if that’s actually what you’re asking about, something very basic like this:
Copy code
val myModule = module {
    single { SomeClass() }
}

class MyClass(val someClass: SomeClass) {
    // someClass will be injected
}
I rarely ever use
get
, but rather lazy injection (if I have to or I get a benefit out of doing it lazily) like
Copy code
class someClass() : KoinComponent { 
    val myInjectedProperty: SomeType by inject()
}
m
yeah that is what I normally do as well @Michael Pohl - thats a good point @arnaud.giuliani agreed, trying to avoid this. I am just fundamentally confused whether extending
KoinComponent
is required for injection into Kotlin classes, because the identically named API (
get
) of course works via
KoinJavaComponent
without the use of additional interfaces
👍 1
m
That’s how I understand the docs at least - unless you pass the injected properties in through the constructor. Since Koin does everything at runtime and (afaik) there’s no reflection used, the functionalities for field injection have to be present in your classes, hence the requirement to extend
KoinComponent
. That’s at least how I understand it. Never used
KoinJavaComponent
, but @arnaud.giuliani’s point is valid.
104 Views