pokeum / debris

Debris (Kotlin Dependency Injection): non-official

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

logo

Debris

Debris is light-weight Kotlin dependency injection library.
Simplified Koin

Copyright 2017 Arnaud GIULIANI, Laurent BARESSE

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Strongly recommend to use Koin instead of Debris

Note

  • Debris does not support Dynamic Modules. (e.g. loadModules, unloadModules)
  • Debris does not support Context Isolation. (Support Global Context Only)
  • Debris does not support Scope.

Table of content

Note
DebrisContext is Global Context

How User Interface Works?

startDebris {               // this: DebrisApplication
    module {                // this: Module
        single {            // this: Debris
            SomeClass()
        }
    }
}
  • The startDebris function

    The startDebris function is the main entry point to launch Debris container. It needs a list of Debris modules to run. Modules are loaded and definitions are ready to be resolved by the Debris container

    Warning
    The modules function needs to called only once inside of the startDebris function

    // start a DebrisApplication in Debris(Global) context
    startDebris {
        // declare used modules
        modules(airbridgeModule)
    }

    The debrisApplication function (Not Recommended)
    // declare a DebrisApplication
    val debrisApplication = debrisApplication {
        // declare used modules
        modules(airbridgeModule)
    }
    // start a DebrisApplication in Debris(Global) context
    DebrisContext.register(debrisApplication)
  • Debris components helps to retrieve our instances outside of the container. Let's take an example.

    A module to define Deeplinker instance

    class Deeplinker
    
    val airbridgeModule = module {
        // Define a singleton for Deeplinker
        single { Deeplinker() }
    }

    we start Debris before using definition.

    Start Debris with airbridgeModule

    fun init(app: Application, config: AirbridgeConfig) {
        // Start Debris
        startDebris {
            modules(airbridgeModule)
        }
    
        // Create Tracker instance and inject from Debris container
        Tracker()
    }

    Here is how we can write our Tracker to retrieve instances from Debris container.

    Use get() & by inject() to inject Deeplinker instance

    class Tracker {
    
        // lazy inject Debris instance
        private val deeplinker by inject(Deeplinker::class.java)
    
        // or
        // eager inject Debris instance
        private val deeplinker = get(Deeplinker::class.java)
    }
  • // declare a Debris and start Debris
    val debris: Debris = startDebris {
        val airbridgeModule = module {
            single { TrackerImpl() as Tracker }
        }
        modules(airbridgeModule)
    }.debris
    
    // retrieve instance from Debris
    debris.get<Tracker>().startTracking()
  • Just use the usual get() function

    class DeviceInfoImpl(
        private val context: Context
    ) : DeviceInfo { }
    
    // Start Debris
    startDebris {
        val androidModule = module {
            single { app }
            single { app as Context }
        }
        val scrapperModule = module {
            single { DeviceInfoImpl(get()) as DeviceInfo }
        }
        modules(androidModule, scrapperModule)
    }
  • class SomeTest {
    
        @Before
        fun setUp() {
            // Start Debris
            startDebris {
                val airbridgeModule = module {
                    single { Deeplinker() }
                    single { MockEventHandler() as EventHandler }
                    single { TrackerImpl() as Tracker }
                }
                modules(airbridgeModule)
            }
        }
    
        @After
        fun tearDown() {
            // Stop Debris
            stopDebris()
        }
    
        @Test
        fun testCaseA() { /* testing case A */ }
    
        @Test
        fun testCaseB() { /* testing case B */ }
    }
3rd party library size Airbridge SDK size

io.insert-koin:koin-core:2.2.2

About

Debris (Kotlin Dependency Injection): non-official


Languages

Language:Kotlin 100.0%