daugeldauge / kinzhal Goto Github PK
View Code? Open in Web Editor NEWCompile-time dependency injection for Kotlin Multiplatform
License: Apache License 2.0
Compile-time dependency injection for Kotlin Multiplatform
License: Apache License 2.0
It would be convenient with library such as Decompose. Decompose Components are created by hand in factory lambda and must be initialized with ComponentContext
provided from parent.
I'll demonstrate with example. Here what we must do with Decompose without auto DI
class RootComponent(
componentContext: ComponentContext
) : ComponentContext by componentContext {
val navigation = StackNavigation<Config>()
val stack =
childStack(
source = navigation,
initialConfiguration = Config.List,
handleBackButton = true,
childFactory = ::createChild,
)
fun createChild(config: Config, componentContext: ComponentContext): ComponentContext =
// Here we must use provided componentContext to create child component
when (config) {
is Config.List -> ListComponent(componentContext, ???) // Where to get depA, depB, depC, etc.???
is Config.Details -> // ...
}
sealed class Config : Parcelable {
@Parcelize
object List : Config()
@Parcelize
data class Details(val itemId: Long) : Config()
}
}
class ListComponent(
componentContext: ComponentContext,
depA: DepA,
depB: DepB,
depC: DepC,
// ...
) : ComponentContext by componentContext {
// ...
}
We can improve this example with assisted injection like so
class RootComponent @Inject constructor(
componentContext: ComponentContext,
listComponentFactory: ListComponentFactory,
// Rest component factories
) : ComponentContext by componentContext {
val navigation = StackNavigation<Config>()
val stack =
childStack(
source = navigation,
initialConfiguration = Config.List,
handleBackButton = true,
childFactory = ::createChild,
)
fun createChild(config: Config, componentContext: ComponentContext): ComponentContext =
when (config) {
is Config.List -> listComponentFactory.create(componentContext)
is Config.Details -> // ...
}
}
@AssistedFactory
interface ListComponentFactory {
fun create(componentContext: ComponentContext): ListComponent
}
class ListComponent @AssistedInject constructor(
@Assisted componentContext: ComponentContext,
depA: DepA,
depB: DepB,
depC: DepC,
// ...
) : ComponentContext by componentContext {
// ...
}
It's possible to create component which provides dependencies with wrong scopes, because there are no such checks here:
Example:
// Unscoped component, or component with some another scope
interface Component(modules = [FooModule::class]) {
...
}
object FooModule {
@Foo // Some scope here
fun provideFoo() : Foo{
...
}
}
This compiles fine and scope will work for Foo
, however it's confusing because this scope is not attached to the current component
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.