Skip to content

dpreussler/toothpick-kotlin-extensions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages