Is there a dependency injection framework for Kotl...
# android
f
Is there a dependency injection framework for Kotlin that works well with Android but also is pretty close to Spring? Dagger 2 seems to be aptly named because it makes me want to poke my eyes out >_<
🔪 1
r
lmfao, there are some that are simlar to DI framework, Let Koin and Kodein. Which will be ok for small project. But if this is a production project I would suggest continue poking your eyes out with Dagger, because its supported by Google, it scales better
âž• 1
d
Scales better @rkeazor? I'm using Kodein in production, I find Dagger a bit cumbersome... apart from compile time checking and a bit of performance, it's harder to use...
g
But also in most cases you will get better architecture with Dagger, because only Dagger provides proper constructor injection and doesn’t leak DI framework implementation details to your business logic. And because Dagger builds graph in compile time it’s of course not so easy to use as reflection based Spring
d
With Kodein you can make a module to do constructor injection.. the only thing that mixes implementation is when
@Inject
is needed, then it uses KodeinAware interface and Kotlin property delegates. Scales better, you're understanding performance wise, is it that much of a difference?
g
But you have manual constructor injection in this case, as I understand and Kodein just provides instances for you.
“Scales better” also means that for big complicated graphs you getting compile time checks, so you don’t get unexpected runtime crashes.
is it that much of a difference
Depends on use case of course. Dagger should be significantly faster than runtime and reflection based Spring. Kodein uses a lot of manual lookups and no injections, so should be fast enough, but still not full featured, comparing to Dagger
d
Thanks for the clarifications! The only thing is that I never really understood the subcomponent docs and how to easily handle android contexts like activities implementing interfaces that need to be passed to the graph in a way that don't cause leaks... they don't bring any good examples and the tutorials I came across were pretty complex or not directed towards my use cases... Do you know of any good resources? Maybe I'll take a look at Dagger2 again 🙂 ...
g
Official tutorials are really basic. it’s true, and it’s big problem if you don’t know what to do. But their Github issues contains a lot of examples and patterns. Main thing you should understand what is responsobility of different dagger APIs and what is Scope (it’s just marker to prevent from mixing of dependencies from different Scopes) There are a few approaches to Android components injection. But I really happy now with dagger-android. You can take a look on this example and talk from @kboyarshinov https://github.com/kboyarshinov/advanceddagger2 https://speakerdeck.com/kboyarshinov/advanced-dagger-2-techniques