Hi there, I've a question: I've this Class below...
# koin
s
Hi there, I've a question: I've this Class below
Copy code
@Factory
class Foo: Mapper<X, Y> {
    override fun convert(data: X): Y {
        return ...
        )
    }
}
and, Mapper interface
Copy code
interface Mapper<in In, out Out> {
    fun convert(data: In): Out
}
I'm using the Koin annotations. When I use the annotation with just, let's say,
@Factory
, Koin automatically maps the dependency to provide the
Mapper::class
. However, I want to specifically map the dependency to the Mapper<X, Y> type. Using the
@Named
annotation achieves my goal, but I'm wondering if there's a better way to do it
a
Doesn't @Factory(binds = Mapper<X,Y>::class) work?
s
No, it shows a compilation error of
Only classes are allowed on the left hand side of a class literal
a
And if you change the interface to an abstract class?
s
The error remains the same
Only classes are allowed on the left hand side of a class literal
😢 , in the above example you shared use case is a interface too
Screenshot 2024-10-07 at 7.02.02 PM.png
a
The binding is done through dsl though
s
So, this use case cannot be achieved via annotations?
a
Ah true, that will work as if you inject just Foo
👍 1
Following works, and you don't need, @named
I think there is a request for this functionality though
Apparently its not possible with annotations
👍 1
s
Following works, and you don't need, @named
Sure, Koin works well when there is only one Foo, but when there are multiple implementation classes, Koin doesn't know which one to inject without the @named annotation.
Then, from the discussion, there are two possible ways to achieve this: 1. DSL 2. Using @named annotation for annotations approach
a
True, @named is always need when you have two or more of the same type, normally you will have one mapper, Mapper<BoundaryModel1, BoundaryModel2>
if you have that, then in any case you need named
3. Is through Factory annotated function like above, you can also provide dependencies as parameters to the function, and annotate it with name if you have more of that type.
👍 1
Hilt also does like that, through @Binds annotation, has the same constraint
👍 1
s
Thanks to @Alexandru Caraus for answering my questions. I'm implementing DI in our production Android project and using Koin annotations, but the changes are currently scattered. Thanks again for the help. I have one more question, which I think is a bit dumb. I'll create a separate thread for it.
a
Np, I also just learned about it 🙂, so thank you too
a
Then, from the discussion, there are two possible ways to achieve this:
1. DSL
2. Using @named annotation for annotations approach
Effectively types are erased at runtime 👍