We are sunsetting Kodein DI. :orange_heart: Kodei...
# kodein
s
We are sunsetting Kodein DI. 🧔 Kodein DI will go into maintenance mode: no new features, but we will keep maintaining the current version and fix issues when necessary. Our focus moves to Inara, a new library I introduced today. Why? I created Kodein in 2015, as the KOtlin DEpendency INjection framework. It was the very first non-JetBrains Kotlin/Multiplatform library to be published. In 2018, Arnaud Giuliani introduced Koin, and by 2020 it was clear Koin was winning the DI library race, thanks to Arnaud's great work and dedication. We discussed internally whether Kodein was still worth maintaining, and we decided to keep it: it still had its loyal user base. Since 2022, however, I have grown to dislike the paradigm that libraries like Kodein and Koin use, so I stopped using them entirely. I want to understand what is going on in my code, and to control how it behaves. A traditional DI container made me uncomfortable: its runtime graph resolution algorithm, and its famous "ask me a type, and I'll give you an instance from context" paradigm. I created Kodein, yet I now believe it to be detrimental to the Kotlin Multiplatform ecosystem. What is different in Inara Inara is a Kotlin Symbol Processor (KSP) that writes a thin layer of interface-based modules for you, resolved at compile time with simple Kotlin syntax. • _No runtime overhead_: a dependency either exists in a module interface, or it does not. • _Explicit discriminators_: you request
NetworkModule.httpClient()
, not just any
HttpClient
. The meaning of a dependency comes from the member's name. • _Composable modules_: modules are interfaces, so
interface AppModule : NetworkModule, DatabaseModule
just works. • _Light presence_: your business classes do not know about the modules.
@DIFactory
builds them from a module for you (no more uggly
KodeinAware
). • Bindings are values, with native support for suspend bindings, extension and overwrite, and per-module scoping. Inara is also opinionated, which is the biggest change in philosophy: no mutable modules, no global modules. With Kodein, I wanted a "universal" tool, and I made concessions along the way (especially by adding new behavior every time I was asked to). A good tool is defined as much by what it does not do as by what it does. In short, I want to write code that is, in order of importance, correct, isolated, idempotent, and only then easy to write. That is why we see more future in Inara: Kodein was my first library, and Inara is the one I would write today. Links • Announcement article: Inara: Inject your dependencies at compile time • Inara on GitHub • Inara documentation Moving to Koin If you like how Kodein worked and how it architected your modules and applications, we warmly encourage you to migrate to Koin. It supports everything Kodein does, and much more, with better optimizations and integrations. Here is a guide to get started: Introducing the Koin migration skills. Thank you A heartfelt thank you to every developer who trusted us with Kodein DI over the years: for using it, for reporting issues, for your contributions and for your patience. Kodein exists because of you, and we are very grateful. I would love to hear what you think of Inara. Questions and feedback are very welcome in this channel!
🧔 2
ā¤ļø 6
i
Do you have an idea on timeline to reach a 1.0 release?
r
I was starting to migrate my projects away from Kodein to Metro for exactly the reasons you state. But I dislike Metro's syntax. Looking forward to digging into Inara.
ā¤ļø 1
s
Not really. We are currently building product with it. That’s our way of battle-testing it. Also, we want other devs to try it and give us their feedback. I’m very happy with the API Inara is exposing, but I don’t want to release 1.0 on my sentiments alone šŸ˜‰
šŸ‘ 1
r
I ran an AI-assisted spike on Inara against my https://github.com/rocketraman/bootable project. The draft PR: https://github.com/rocketraman/bootable/pull/65. Here are the migration notes: • Multibindings are the big gap, Bootable runs a
Set<AppService>
, and library modules (JDBC pool, Flyway, Exposed) each add a service to it. ā—¦ Kodein: the library opens the set, each module adds its own service, and the app just imports the modules. ā—¦ Inara: the app has to list every service itself, the libraries’ ones too. Forgetting one still compiles, and that service never starts. • Libraries can’t ship default bindings. ā—¦ Kodein: a library module binds its singletons itself. ā—¦ Inara: the library can only offer bindings from its companion, because a getter default runs on every access and can’t hold a singleton. So the app passes every binding by hand. ā—¦ Suggestion: let a parent interface declare a default
Binding
that the generated builder uses as the parameter’s default. That would shrink app modules a lot. • One flat namespace, and no qualifiers or optional bindings. ā—¦ Kodein: bindings are keyed by type, and optional ones are a simple `instanceOrNull`. ā—¦ Inara: all parents’ members merge into the app’s module, so library members need prefixed names (
startOrderTiebreaker
), and ā€œoptionalā€ means a getter default. ā—¦ Kodein also reads the tiebreaker from config automatically when a
ConfigBinder
is bound. With Inara the app has to opt in explicitly. • What worked well: ā—¦ A
@DIModule
collects members from parent interfaces that aren’t annotated, so libraries need no KSP. ā—¦ The contravariant
Binding<M, …>
lets a library binding typed on its own interface plug straight in. ā—¦ Diamond inheritance works, and a missing binding is a compile error. • Minor: the runtime is built with Kotlin 2.4 metadata, so consumers need at least a Kotlin 2.3 compiler.
s
Amazing feedback, thanks a lot @rocketraman ! Libraries default bindings: You can compose modules with `@DIModule(composed = true)`. So Libraries can offer their own modules. Optional bindings, this can easily be achieved with the combination of composed modules, default implementations and copying a module. However, the pattern of ā€œrequesting a typeā€ is precisely what Inara will not do, so you still need to declare a method (or property) in a module with a default
null
value. I agree that this is not on par with Kodein, but that is by design. Multibindings: This is tricky, because it touches on precisely what we decided Inara was not: a runtime service locator. What you can do with Inara right now, with composable modules, is create a set and have module creation functions that fill the set. Here's an example on how to do that. The whole point of Inara is to have everything resolved at compilation only via generated code. So what we could add is a compile-time locator pattern. This would require you to create a custom annotation for located services. Here is a sample proposal. What do you think? Kotlin 2.4 metadata: the first release version may be compatible with Kotlin’s new companion blocks (I’m still considering it), so it may be released with Kotlin 2.5 metadata.
šŸ‘ 1
r
Thanks @salomonbrys, that was really helpful. I’ve reworked the prototype around composition: https://github.com/rocketraman/bootable/pull/65 • Composed modules:
@DIModule(composed = true)
works, including a composed module used as the parent of another. But I’ve run into the diamond inheritance restriction documented at https://kosi-libs.org/inara/0.2/core/composition.html#_restrictions. The current workaround I’ve implemented is that each module takes it as a builder parameter instead. • Optional bindings: these are now builder parameters with defaults. That works fine. • Multibindings: For now, I’ve implemented your suggested approach with some minor tweaks — see below for my comments on the compile-time locator proposal. On diamonds: I see they’re rejected currently and I get why. But the module declarations become quite a bit less elegant because of the need to manually pass in the dependent modules rather than Inara handling the dependency. What if the user could declare ā€œpartialā€ modules that can only be composed? KSP would then require the composed module to supply that ancestor exactly once, e.g. through another parent. That stays fully compile-time, since there’s never a second instance. Example: Example from Bootable — here the Flyway and Exposed modules both declare the
JdbcDataSourceModule
parent they need without building it. A partial module can’t be built on its own, only composed. When KSP generates
AppModule
, it requires exactly one parent to supply each unbound parent of a partial parent. If none or more than one do, that’s a compile error. There’s never a second
JdbcDataSourceModule
, so
dataSource
is never ambiguous. This would simplify my
AppModule
declaration a lot, getting it close to the Kodein DSL in simplicity and clarity. > So what we could add is a compile-time locator pattern. This would require you to create a custom annotation for located services. Here is a sample proposal. The compile-time locator proposal makes sense, with two considerations: 1. In Bootable, the
@AppServices
members are in published library jars, and only the app runs KSP, so the processor has to read the annotation from the classes on the classpath. 2. Only the composed module can call
locateAppServices()
, but in our case its the framework parent
BootableModule
that needs the set, and child modules contribute to it. Could the locator implement a declared member instead? For example,
BootableModule
declares
@AppServices val appServices: Set<AppService>
, and the composed module’s generated code fills it in from its parents’
@AppServices
members. That’s essentially Metro’s `@IntoSet`: a set whose members are all known at compile time. If you have time, I’d appreciate a review of the draft PR, especially whether the library modules use Inara the way you intended. Some key files are: • boot-config-common/src/inara/.../ConfigModule.kt • boot/src/inara/.../BootableModule.kt and Acceptors.kt • boot-db-jdbc/src/inara/.../JdbcDataSourceModule.kt • boot-db-flyway/src/inara/.../FlywayMigrationModule.kt and boot-db-exposed/src/inara/.../ExposedDatabaseModule.kt • examples/server-ktor-db/inara/src/main/kotlin/App.kt You can find all the Kodein and Metro equivalents by replacing
inara
in the paths with
kodein
and
metro
. There is also the
vanilla
no-DI version of the example to directly see the relationships. • Kodein App.kt • Vanilla App.kt
a
Thanks a lot for all your work on Kodein and for everything you’ve brought to the Kotlin community @salomonbrys @romainbsl šŸ™ Kodein paved the way for DI in Kotlin. Let me know if anything in the Koin skills needs adjusting.
ā¤ļø 2
r
Meta-question: is the name of this channel going to change to #inara or #kosi-libs? Should discussion about inara continue here, or elsewhere?