Dependency Injection library for Kotlin Multiplatform, support iOS and Android
Lightweight dependency injection framework for Kotlin Multiplatform application
Dependency injection (DI) is a software design pattern that implements Inversion of Control (IoC) and Service Locator for resolving dependencies. It could be implemented many ways. More info about DI here
Multiplatform-DI helps you to use DI in your KMM application easily, to provide shared common architecture solution that works both in iOS and Android native applications. Yes! It works even with iOS, in all cases.
Just add a dependency into your build.gradle.kts in shared module. Available in maven central.
//build.gradle.kts
repositories {
mavenCentral()
}
/*....*/
allprojects {
repositories {
google()
mavenCentral()
maven(url = "https://jitpack.io")
}
}
dependencies {
implementation("io.github.anioutkazharkova:di-multiplatform-lib:1.2.0")
}
Check the ready-mady solution here It is a small client for Movies DB API with common BL and architecture
You can also use this KMM DI template to create your app with integrated DI. More info in wiki
Or you can also integrate if all by yourself. Just follow next steps:
class DIManager {
val appContainer: DIContainer by lazy { DIContainer() }
fun<T:Any> resolve(type: KClass<T>):Any? {
return appContainer.resolve(type)
}
fun<T:Any> addToScope(scope: ScopeType, type: KClass<T>, fabric: ()->T?) {
appContainer.register(type, ScopeType.Graph,fabric)
}
}
class ConfigurationApp {
val appContainer: DIManager = DIManager()
init {
setup()
}
fun setup() {
//register hear all your components from shared module
}
}
fun setup() {
appContainer.addToScope(ScopeType.Container,NetworkClient::class) {
NetworkClient()
}
appContainer.addToScope(ScopeType.Container,MoviesService::class) {
val nc = appContainer.resolve<NetworkClient>(NetworkClient::class) as? NetworkClient
MoviesService(nc)
}
}
fun setup(di: DIManager) {
this.moviesService = di.resolve<MoviesService>(MoviesService::class) as? MoviesService
print(moviesService)
}
@HiltAndroidApp
class App : Application() {
companion object {
val config = ConfigurationApp()
val container = config.appContainer
lateinit var INSTANCE:App
var AppContext: Context? = null
}
override fun onCreate() {
super.onCreate()
INSTANCE = this
AppContext = this
}
}
class Util{
static var shared = Util()
private lazy var config: ConfigurationApp = {
return ConfigurationApp()
}()
lazy var container: DIManager = {
return config.appContainer
}()
}
You can also use some decorations to your code (property wrappers, delegates and ets). Sample you can see here