Di Multiplatform Lib Save

Dependency Injection library for Kotlin Multiplatform, support iOS and Android

Project README

Maven Central supports iOS supports Android 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) 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.

Supports

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

Features

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

Future

  • KSP injection
  • Circullar dependencies

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

Sample

Check the ready-mady solution here It is a small client for Movies DB API with common BL and architecture

Basic usage (good practice)

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:

  1. First, you need to enable an access to DI library from native apps, because their is no direct access to dependencies in shared module. You need create a class to manage registration and resolution of objects with DIContainer. It should be placed in commonMain of your shared module:
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). Also place it in commonMain:
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 (also commonMain):
 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. Now you can resolve components from any place of your shared part:
fun setup(di: DIManager) {
        this.moviesService = di.resolve<MoviesService>(MoviesService::class) as? MoviesService
        print(moviesService)
    }
  1. Next, create a property to get access container in Android native app:
@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. Repeat same for iOS:
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

Open Source Agenda is not affiliated with "Di Multiplatform Lib" Project. README Source: anioutkazharkova/di-multiplatform-lib

Open Source Agenda Badge

Open Source Agenda Rating