Replies: 2 comments 5 replies
-
Hi @fmatosqg let me share here my vision, and I can share more about how I see things. Let's take the thermosiphon case, it's a compact example. // Heater component
interface Heater
class ElectricHeater : Heater { ... }
// Pump component
interface Pump {
fun pump()
}
// Is injecting the Heater component
class Thermosiphon(private val heater: Heater) : Pump { ... }
// Coffee Maker
// Is injecting the Heater & Pump components
class CoffeeMaker(private val pump: Pump, private val heater: Heater)
// is bootstraping the CoffeeMaker instance
class CoffeeApp : KoinComponent {
val maker: CoffeeMaker by inject()
} For the Kotlin plugin perspective (either KSP or Meta), I see 2 ways of doing it. #1 - First, would be a smarter DSL, thanks to code generation behind. I would write a module like: val coffeeAppModule = module {
single<CoffeeMaker>()
single<Thermosiphon>() bind Pump::class
single<ElectricHeater>() bind Heater::class
} The experience would be the same as using #2 - Annotation processing solution, that generates code behinds for us. We would keep the "DSL experience" but with annotation. As a start, I would have a I would write something like below, only on concrete types that need to be declared: @Single
class ElectricHeater : Heater { ... }
@Single
class Thermosiphon(private val heater: Heater) : Pump { ... }
@Single
class CoffeeMaker(private val pump: Pump, private val heater: Heater) Each In all solution, I believe we need to see if we can generate some "default" code somewhere and allow us to run it. Let's see what we can do with those Kotlin plugin technologies. |
Beta Was this translation helpful? Give feedback.
-
Upgrade of the POC to ksp beta01 is completed (branch https://github.com/fmatosqg/koin/tree/ksp) . I'm still using a new class called
|
Beta Was this translation helpful? Give feedback.
-
I'd like to get some chat going on about how to make koin better by using ksp. I understand that arrow-meta is much more powerful, at the same time that in my experience it's slower at compile time and a bit harder to figure out how to use and troubleshoot.
#1059 was started to explore what things can be done with ksp. So far it has shown that it's very easy to find classes, constructors and methods that have annotations, and the generation of factories, singletons, etc is relatively straighforward as long as it doesn't cross gradle module boundaries.
From my explorations, doing the same task without annotations is either impossible or more complicated - though this is not complete yet. One way that is yet to be explored is through the application developer writing an interface for a factory, and ksp generating code that glues well with koin modules. For example:
Although it's not clear how class inheritance would be solved by either the "interface" or "annotation" approaches. Though it's not a problem of code generation (it's easy to crawl all superclasses and generate factory chains that resolve all the class hierararchy of
CoffeeMaker
), but finding the best defaults and allowing them to be easily overriden by the application developer.Something that could work but I haven't tried:
fun providesEngine<Car>() : Engine
could resolveinject<Engine>
given an instance ofCar
is somewhere in the dependency graph.To start small, I think these are ways to bulk generate a partial graph, while the more complicated use cases could be filled in by the application developer.
Beta Was this translation helpful? Give feedback.
All reactions