Hello everyone, I have a beginner question _(I am ...
# koin
j
Hello everyone, I have a beginner question (I am new to
Koin
)
. I was reading the docs specifically on naming:
Copy code
val myModule = module {
    single<Service>(named("default")) { ServiceImpl() }
    single<Service>(named("different")) { ServiceImpl() }
}

val service : Service by inject(qualifier = named("default"))
Now I was wondering how would I do this for constructor injection? I am not using any of the annotation (
KSP
) features. Desired outcome something like:
Copy code
module { singleOf(::SomeClass) }

class SomeClass(private val defaultService: Service, private val differentService: Service) {}
a
Better have separate modules with defaults and specific modules for test
Then you dont need so many named qualifiers
If there is one dependency of a type its the default one, doesnt need the qualifier.
So keep the app module with defaults
Then in test start koin with modules from the app and modules created in test for yor specific test, you can combine them nicely
I do it like that and it works well
And its pretty flexible
j
This was the example from the tutorial, I wasn't very clear but, testing doesn't have anything to do with it, I want the different impls of the same class injected. I updated my original question for more clarity
For example in
Spring Boot
I would do something like:
Copy code
class SomeClass(
    @Qualifier("default") private val default: Service,
    @Qualifier("different") private val different: Service,
) {}
or in
Android + Dagger/Hilt
Copy code
@Qualifier
annotation class default

@Qualifier
annotation class different

class SomeClass(
    @default private val default: Service,
    @different private val different: Service,
) {}
🙌 1
If anyone reads this I found this that solved it for me: https://github.com/InsertKoinIO/koin/issues/109
w
Don’t use strings, you’re setting yourself up for turbo-pain
Use enums or sealed classes instead