Hey everyone :wave::skin-tone-2: we’re trying out ...
# koin
c
Hey everyone 👋🏻 we’re trying out Koin for a project and are coming from Dagger 2. I’m wondering if there is any equivalent for
@Inject constructor(...)
in Koin? I’d like to avoid defining every dependency in modules/components but unsure if that’s possible.
c
Yes, there is experimental support for this in
koin-core-ext
artifact. There are additional DSL functions which inject the primary constructor via reflection (don't need the
@Inject
annotation) https://insert-koin.io/docs/2.0/documentation/reference/index.html#_extensions_experimental_features
m
(I just started typing that there's no support for that 😄)
c
🙇🏻 awesome thank you so much!
@Casey Brooks actually I’m not sure if this is what I was looking for. It looks like you still have to define it in a Module?
m
I personally prefer not marking constructors as it mixes up production code with DI framework's elements. Using only modules allows for easy switching of DI frameworks/libs
You still have to define that class is part of DI tree but you don't have to call constructor explicitly
You'd still have to to that for every class used as interface with @Binds in Dagger
c
@Cody Engel Sorry, i misread the original question. Yes, I believe you will still need to register each class in a module, Koin won't automatically register classes as it constructs the graph. But it does make it significantly simpler to register classes
c
Gotcha, we’re going to roll with it and see how we feel. I personally liked just defining the top level dependencies and then allowing other classes to just do their injection through the
javax
Inject annotation, I think it minimized the amount of ceremony. I agree though, Koin makes it far easier to register things and get up and running.
c
You might be able to use ClassGraph to find those annotations and build those modules dynamically (https://github.com/classgraph/classgraph). It also supports build-time scanning for use on Android or for minimal runtime impact (https://github.com/classgraph/classgraph/wiki/Build-Time-Scanning)
a
also use
koin-android-ext
or
koin-androidx-ext
if you want automatic constructor injection for ViewModel