Skip to content

alexwestphal/kompat-hk2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kompat-hk2

A Kotlin compatibility layer for constructing HK2 Bindings and using HK2 Service Locators.

Download

Binding

kompat-hk2 provides slightly nicer syntax for programmatically constructing HK2 bindings.

// Before
val myBinder = object: AbstractBinder() {
    override fun configure() {
        bind(B::class.java).`in`(Singleton::class.java).to(A::class.java)
        
        bindAsContract(C::class.java).`in`(PerThread::class.java)
    }
}

// With kompat-hk2
val myBinder = binder {
    bind(B::class) inScope Singleton::class toContract A::class
    
    bindAsContract(C::Class) inScope PerThread::class
}

Getting Services

kompat-hk2 provides slightly nicer syntax that allows for the type to be inferred in some cases.

// Before
val clock: Clock = serviceLocator.getService(Clock::class.java)

// With kompat-hk2
val clock: Clock = serviceLocator.getService()
// or
val clock = serviceLocator.getService<Clock>()

Maven

Configure the kompat repository

<repository>
    <id>bintray-ahwnz-kompat</id>
    <name>bintray-kompat</name>
    <url>https://dl.bintray.com/ahwnz/kompat</url>
</repository>

Add the dependency

<dependency>
    <groupId>nz.ahw.kompat</groupId>
    <artifactId>kompat-hk2</artifactId>
    <version>{version}</version>
</dependency>