Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing .bind for multiple classes sharing the same interface #2076

Open
ArthurKun21 opened this issue Dec 13, 2024 · 3 comments
Open

Missing .bind for multiple classes sharing the same interface #2076

ArthurKun21 opened this issue Dec 13, 2024 · 3 comments
Labels
core question Usage question
Milestone

Comments

@ArthurKun21
Copy link

Describe the bug
Multiple classes sharing the same interface should be able to be called.

To Reproduce
Steps to reproduce the behavior:

  1. Create multiple classes sharing the same interface
  2. use getKoin() and get<ClassName>() fails
  3. search for getKoin() and unable to find the .bind<>()

Expected behavior
Be able to bind the same interface with multiple classes

Koin module and version:
koin-core 4.1.0-Beta1

Snippet or Sample project to help reproduce
https://insert-koin.io/docs/reference/koin-core/definitions/#additional-type-binding

val myModule = module {

    // Will match types ServiceImp & Service
    single { ServiceImp() } bind Service::class
}

Note here, that we would resolve the Service type directly with get(). But if we have multiple definitions binding Service, we have to use the bind<>() function.

and I did found an old stackoverflow with what near outcome of what I want.

https://stackoverflow.com/a/60508841/14859274

single { AValidator() } bind IValidator::class
single { BValidator() } bind IValidator::class
single { CValidator() } bind IValidator::class
single { DValidator() } bind IValidator::class
val validator: IValidator = getKoin().bind<IValidator, AValidator>()

But instead I want to be able to get the AValidator

val validator: AValidator

since in the project I was using, we were using delegate interface

class AutoDetect(
    api: IFgoAutomataApi,
) : IFgoAutomataApi by api {

and wanted to call them by their class name.

@arnaudgiuliani
Copy link
Member

You have the binds operator for that, taking a collection of type

@arnaudgiuliani arnaudgiuliani added the question Usage question label Dec 16, 2024
@ArthurKun21
Copy link
Author

I guess it might be issue with the delegate then.

InsertKoinIO/koin-annotations#35

@arnaudgiuliani arnaudgiuliani added this to the 4.1.0 milestone Jan 16, 2025
@arnaudgiuliani
Copy link
Member

do you have complete snippet to help reproduce?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core question Usage question
Projects
None yet
Development

No branches or pull requests

2 participants