https://kotlinlang.org logo
#kotlin-inject
Title
# kotlin-inject
e

evant

04/21/2022, 8:55 PM
Not currently for simplicity's sake. Did look at a bit but haven't found a way that has enough benefits to be worth saving just a line of code. If you are binding an impl to an interface the it's probably because you want to be using different impls in different places so you need to tie it to something, it can't just be global. Putting it in a component is a natural way to do that as you can have different components provide different impls for the same interface. Sure you could reverse it and do something like
Copy code
@Binds(in = AppComponent::class)
@Inject
class MyImpl : Impl
but it just feels like shuffling things around? It's also nice to have them all in one place so you can override them all for tests (a common usecase for having multiple impls for an interface).
m

muthuraj

06/25/2022, 2:58 PM
Hi, just want to share my use case regarding this.
If you are binding an impl to an interface the it's probably because you want to be using different impls in different places
Almost all classes in my project are single implementation interfaces. I'm using this pattern to create fakes easily in unit tests. In my current Android project, I use Anvil to solve this issue.
e

evant

06/29/2022, 7:17 PM
I'm using this pattern to create fakes easily in unit tests
So you do have multiple implementations, the real one and the fake 😉 You can totally use kotlin-inject in your tests to set things up passing in the fake as I describe here. Though it's understandable if you want to do manual di in tests instead.
2 Views