An open API service indexing awesome lists of open source software.

https://github.com/jlleitschuh/kotlin-guiced

Convenience Kotlin API over the Google Guice DI Library
https://github.com/jlleitschuh/kotlin-guiced

dependency-injection google google-guice guice java jvm kotlin

Last synced: 3 months ago
JSON representation

Convenience Kotlin API over the Google Guice DI Library

Awesome Lists containing this project

README

          

# Kotlin Guiced

[![Build Status](https://travis-ci.org/JLLeitschuh/kotlin-guiced.svg?branch=master)](https://travis-ci.org/JLLeitschuh/kotlin-guiced)
[ ![Download](https://api.bintray.com/packages/jlleitschuh/maven-artifacts/kotlin-guiced/images/download.svg) ](https://bintray.com/jlleitschuh/maven-artifacts/kotlin-guiced/_latestVersion)

A Kotlin API wrapper over the [Google Guice](https://github.com/google/guice) Dependency Injection library.

This library aims to encourage the use of Guice with Kotlin by simplifying the Guice API so it is more
fluent in the Kotlin programming language.

## NOTE:
Project is in very early stage of development. I plan to add helper functions as needed in a parallel cooperate internal
project and this project it may not comprehensively cover all of the methods out of the box.

## Examples:

### TypeLiteral
Because of java type erasure, Guice uses some strange java syntax to preserve type at runtime.
Many of these problems have been solved by Kotlin using inline functions with `reified` types.

In java you can declare a type literal with:
```java
final TypeLiteral> someLiteral = new TypeLiteral>() {}
```
In Kotlin this syntax becomes even more verbose requiring more characters to write.
```kotlin
val someLiteral = object : TypeLiteral>() {}
```
This library provides helpers like the one below that is much cleaner to read.
```kotlin
val someLiteral = typeLiteral>()
```

### Guice Modules

Creating a module in Java requires quite a bit of extra boilerplate.
```java
public class MyModule extends AbstractModule {
@Override
void configure() {
bind(SomeService.class).to(SomeServiceImpl.class);
}
}

class Main {
public static void main(String... args) {
final Injector injector = Guice.createInjector(new MyModule());
}
}
```
This is the equivalent in Kotlin:
```kotlin
fun main(vararg args: String) {
val myModule = module {
bind(SomeService::class).to(SomeServiceImpl::class)
// Or, even simpler with reified generics
bind().to()
}
val injector = Guice.createInjector(myModule)
}
```

The library also defines a simple way of declaring private modules:
```kotlin
fun main(vararg args: String) {
val privateModule = privateModule {
bind().to()
expose()
}
val injector = Guice.createInjector(privateModule)
}
```

## Project Structure
The intention is to structure this project such that Guice Core and each of it's respective extensions will
be in their own projects. The reasoning being that a library consumer can choose to depend upon only the Guice
extensions they need an not get a transitive dependency on a Guice extention they don't need.

## Developers

### Requirements
Requires JDK 8 installed (Kotlin Compiler compiles to JDK 6 bytecode but requires JDK 8 to run).

### Building

This project uses Gradle to build/test/deploy code.
Run `./gradlew tasks` to se the various tasks this project supports.