4 ms·
I advise to try build-time DI frameworks, like Dagger, see how it generates the glue code, and then ditch it and write the same glue code yourself. Yes, build-
by deepsun 4y ago
I advise to try build-time DI frameworks, like Dagger, see how it generates the glue code, and then ditch it and write the same glue code yourself.
Yes, build-time cannot do tricky cases (when it depends on some runtime-only thing), but I'm yet to see any case of that.
Build-time is also easier on JIT compiler, as there's no reflection involved in runtime, for VM it's all just hardcoded.
- jillesvangurp 4y agoI prefer koin instead. It does no magic. It's simple function calls packaged up as a nice Kotlin DSL. Pure declarative. Easy to debug. It does not even use reflection. I've used it with ktor, and with kotlin-js in a browser (it's a kotlin multi platform library). There's basically no overhead relative to the code you'd otherwise be writing manually. I'm not an Android developer but I hear it's pretty popular there as well. I've use spring dependency injection as well. It's actually not that bad if you use constructor injection only. No Autowired in any code I touch. You don't need it. Constructor injection makes everything easy. And it makes it easy to test as well. My unit tests do not depend on Spring. There's no need. And with the recent declarative way of creating beans, it can be pretty similar to koin. I've done some diy dependency injection as well on occasion. It's not that hard. Just separate your glue code (construction) from your logic. Your main function would be a good place. Constructors don't get to do work. I've seen some bad frontend code that violate these rules and it's a mess where nothing is testable because trying to run any bit of code you end up with half the code base firing up. Lack of a framework is no excuse for bad design.
- deepsun 4y agoKoin is all about Android. I tried it briefly, but all the examples don't work for me, e.g. 'by inject()` looks cool, if it worked, but it doesn't, because it requires a Class parameter. Inconveniently, no examples show what to "import" as well.