mirror of
https://github.com/InsanusMokrassar/SDI.git
synced 2024-11-08 09:23:57 +00:00
Simple dependency injection, based on KotlinSerialization
gradle/wrapper | ||
src | ||
.gitignore | ||
build.gradle | ||
CHANGELOG.md | ||
gradle.properties | ||
gradlew | ||
gradlew.bat | ||
LICENSE | ||
maven.publish.gradle | ||
mpp_publish_config.json | ||
publish.gradle | ||
README.md | ||
settings.gradle |
SDI
It is simple (in comparison with other systems) DI, which aim to be compatible and predictable.
Platforms support
- JVM
- JS
- Native
Required environment
To use this library you will need two things:
- Json serializer
- Json config
Unfortunately, currently not supported other formats (due to issue in Kotlinx.serialization)
Format of config
Full examples of usage you can find in tests. In two words, there are a few rules for constructing of config:
- Config root is an Json Object
- Config root names will be used as dependency names
- In the config dependency names can be used everywhere
- In places, where dependency will be injected, must be used
@ContextualSerializer
annotation or@Serializer(ContextSerializer::class)
Examples
Lets imagine, that we have several interfaces and classes:
package com.example
// ... //
interface ControllerAPI {
fun showUp()
}
interface ServiceAPI {
val names: List<String>
}
@Serializable
class Controller(@ContextualSerialization val service: ServiceAPI) : ControllerAPI {
override fun showUp() {
println("Inited with name \"${service.names}\"")
}
}
@Serializable
class BusinessService(override val names: List<String>) : ServiceAPI
Here there is common way to create all of this directly:
val service = BusinessService(listOf("One", "Two"))
val controller = Controller(service)
And with config for this library:
{
"service": [
"com.example.BusinessService",
{
"names": ["One", "Two"]
}
],
"controller": [
"com.example.Controller",
{
"service": "service"
}
]
}
List example you can find in this test. Besides, usually you can create objects inside of places where expected something like dependency injection directly. In this case config will look like:
{
"controller": [
"com.example.Controller",
{
"service": [
"com.example.BusinessService",
{
"names": ["One", "Two"]
}
]
}
]
}
More expanded example you can find in suitable test.