dpreussler / toothpick-kotlin-extensions

Kotlin sprinkles over Toothpick dependency injection library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CircleCI

Toothpick Kotlin extensions

Kotlin sprinkles over Toothpick dependency injection library

define your modules like this:

module {
    bind<Repository>(DataBaseRepository::class)
    bindInstance<Scheduler>(DefaultScheduler())
    bindProvider(ApiProvider::class)
    bindProviderInstance(ApiProvider())
    bindProviderInstance<Api>{ RestApi() }
}

Details

defining modules

Instead of

Module().apply {
    // bindings
}

use this method with a lambda

module {
    // bindings
}

Or if you have a single module, you can directly use the bindings in the scope definition:

simpleScope(SCOPE) {
    // bindings
}

or add multiple modules:

scope(SCOPE, {
    module {
        // bindings
    }
    module {
        // bindings
    }
})

binding to classes

Instead of writing

   module.bind(Repository::class.java).to(DbRepository::class.java)       

Use generics

   module.bind<Repository>().to(DbRepository::class.java)  

Or write it in one instruction and use the Kotlin KClass instead of Java Class type

   module.bind<Repository>(DataBaseRepository::class)

binding to instances

Instead of writing

   module.bind(Scheduler::class.java).toInstance(DefaultScheduler())
   

Use generics:

    bindInstance<Scheduler>{ DefaultScheduler() }   

binding to providers

   module.bind(Api::class.java).toProviderInstance(ApiProvider())

use type inference (as provider is already generic):

    bindProvider(ApiProvider::class)
    
    bindProviderInstance(ApiProvider())

and you can even directly pass the lambda creating the instance so no need to implement provider

    bindProviderInstance<Api>{ RetrofitApi() }

Gradle

repositories {
    maven {url "https://jitpack.io"}
}

compile 'com.github.sporttotal-tv:toothpick-kotlin-extensions:0.2.2'	

About

Kotlin sprinkles over Toothpick dependency injection library

License:Apache License 2.0


Languages

Language:Kotlin 100.0%