DI Kotlin Multiplatform Library


License

License

MIT
Categories

Categories

ORM Data
GroupId

GroupId

io.github.anioutkazharkova
ArtifactId

ArtifactId

di-multiplatform-lib
Last Version

Last Version

1.0.4.5
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

DI Kotlin Multiplatform Library
DI Kotlin Multiplatform Library
Project URL

Project URL

https://github.com/anioutkazharkova/di-multiplatform-lib
Source Code Management

Source Code Management

https://github.com/anioutkazharkova/di-multiplatform-lib

Download di-multiplatform-lib

Dependencies

compile (1)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib-common jar 1.4.32

Project Modules

There are no modules declared in this project.

Maven Central GitHub tag (latest SemVer pre-release) GitHub last commit License: MIT GitHub stars GitHub forks GitHub watchers GitHub followers

Twitter URL

Buy Me A Coffee donate button

Multiplatform-DI library for Kotlin Multiplatform

Lightweight dependency injection framework for Kotlin Multiplatform application

Dependency injection (DI) is a software design pattern that implements Inversion of Control (IoC) for resolving dependencies. 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.

Supports

  • iOS (Kotlin Native)
  • Android (Kotlin JVM)

Features

  • Thread safety
  • WeakReference
  • Singletones (no extra actions in iOS side)
  • Scopes
  • Easy register and resolve components

Installation

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.0.4.5")
  }

Basic usage (good practice)

  1. In case, you need to provide an access to DI library from native apps, create a class to manage registration and resolution of objects with DIContainer:
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)
    }
}
  1. Provide common configuration (used for both common and native apps):
class ConfigurationApp {
val appContainer: DIManager = DIManager()

   init {
       setup()
   }

    fun setup() {
       //register hear all your components from shared module
    }
}
  1. Register 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)
        }
    }
  1. Resolve components from any place:
fun setup(di: DIManager) {
        this.moviesService = di.resolve<MoviesService>(MoviesService::class) as? MoviesService
        print(moviesService)
    }
  1. Call container from Android:
@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
    }
}
  1. And from iOS:
class Util{
    static var shared = Util()
    private lazy var config: ConfigurationApp = {
       return ConfigurationApp()
    }()
    
    lazy var container: DIManager = {
        return config.appContainer
    }()
}

Versions

Version
1.0.4.5