what library do you use to di in kotlin ?
# android
a
what library do you use to di in kotlin ?
๐Ÿฟ 5
e
koin #koin
a
Dagger2, but probably not the best choice. Weโ€™re using it since weโ€™re working on a big legacy project and itโ€™s not easy to change
k
i haven't read anything that's lead me to believe Dagger is not a good choice
๐Ÿ‘ 11
a
@Efe i started to use #koin , it's so easy and i'm a little worried
a
Toothpick
a
@Kris Wong dagger2 is definitely a good choice but the initial setup and all that code is the real pain.
t
just kotlin ๐ŸงŒ
k
dagger2 definitely has a learning curve. i'd call it steep.
๐Ÿ‘ 6
a
@Andy Gibel i should check that out.
@tapchicoma ofc but, not when you're taking unit tests seriously.
t
why not? You could still provide dependencies through constructor params without any DI framework
๐Ÿ‘ 2
a
@Ahmed Mamdouh wait about a month and the new, kotlin first, version will be released publically ๐Ÿ™‚
๐Ÿ‘ 1
a
@tapchicoma aha , got you sorry
@Andy Gibel i already like what i am reading about it , and how safe it is , thank you ! ๐Ÿ™‚
g
I work on a huge project with around 130 Gradle modules and honestly Dagger is the only viable choice for us. Furthermore Koin is a service locator, not a dependency injector
๐Ÿ‘ 2
e
that is right. It doesnโ€™t have to use di pattern to be useful ๐Ÿ˜„ it is a charm for small-medium size projects.
a
@Giorgio Antonioli thank you for clearing that up !
e
for large projects runtime injections would hurt though
๐Ÿ‘ 3
m
None. Tried multiple times. Always more hassle to maintain and debug than it does actually solve problems. imo not needed in most projects until they become huge.
๐Ÿ‘ 2
๐Ÿ‘Ž 2
a
@Marc Knaup what i like about di is that you don't recreate/instantiate objects every time you need them , you just inject them to classes , it's a good practice that leads to a good architecture and a clearer code , if you opened any class you'd find that any foreign class is being injected in the same pattern , that way it's easier to read , it's just building the di framework can be hassle at first but it's worth it.
l
opinion time: large project dagger, small project koin. best to form your own opinion tho! ๐Ÿ™‚
๐Ÿ‘ 4
m
@Ahmed Mamdouh I also create them only once or per transaction, whatever makes sense in a given context. This is not unique to DI. You can have pretty clear code without DI too. In my experience that code was always more navigable because you can use all IDE and static tooling and basic Kotlin knowledge to understand what your code is doing, when objects are created and where they live. Try doing the same with Dagger. Once you have a bunch of modules and components it can still work, but once something breaks unexpectedly you'll have a hell of a debugging session. Usually if my project's dependencies become complex I start refactoring to simplify the project structure and flatten the dependency tree rather than adding more tools to manage that complexity. Bonus points: As it's still plain old Kotlin new developers in the project don't have to learn any DI library and it's annotations, classes, functions, methodology and quirks first. They just need their Kotlin knowledge. I won't judge DI in general as there are so many different types of projects at all sizes - it can have its merits. Just in all Kotlin projects I've worked on so far - mostly backend systems of varying size, Android apps and general libraries - it just wasn't needed nor useful and sometimes detrimental. As with everything when programming: Whether something is useful or not depends on the context.
a
I just said, that Dagger might not be the best choice. Depending on your use case. Dagger is a very mighty framework and provides solutions for many different use cases. Koin or Kodein might be simpler. Also keep in mind that annotations slows down your build times
j
I'm not sure that Dagger is more complex unless you're specifically talking about dagger-android. If not ready for Dagger just use constructor injection+homegrown solution. At same point the benefit of not maintaining the boilerplate yourself will become obvious
a
@Marc Knaup ok , i understand your point , from where you stand right now di seems useless and just a burden , and i feel the same sometimes, when i said clearer code i meant having a common ground where all developers using di understand where goes where instantly.
@Marc Knaup i mean , i saw developers using extensions differently , kotlin is so wide and expressive which makes it slightly hard to have a common and perfectly set way , that's why i respect what you are saying you shouldn't use anything just for the sake of it.
m
@Jeremy yeah my experience with Dagger is mostly Android. There are indeed many ways to do dependency management even without DI frameworks. Good point that without DI framework devs will come up with different solutions. So there should be a strong per-project convention at least ๐Ÿ™‚
j
I know this is #android but I've been experimenting and thinking of DI patterns that work across iOS/Android. iOS obviously doesn't have Dagger so currently investigating the manual injection route while Android assembles dependencies using Dagger
Developing good abstractions, separations is most important and how everything assembled less important imo
๐Ÿ‘ 1
If your implementation is dependent on a DI library that is def a code smell
a
@Marc Knaup exactly ๐Ÿ™‚