Skip to content

Latest commit

 

History

History
89 lines (71 loc) · 3.05 KB

README.md

File metadata and controls

89 lines (71 loc) · 3.05 KB

Kotlin Guiced

Build Status Download

A Kotlin API wrapper over the 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:

final TypeLiteral<Map<Integer, String>> someLiteral = new TypeLiteral<Map<Integer, String>>() {}

In Kotlin this syntax becomes even more verbose requiring more characters to write.

val someLiteral = object : TypeLiteral<Map<Integer, String>>() {}

This library provides helpers like the one below that is much cleaner to read.

val someLiteral = typeLiteral<Map<Int, String>>()

Guice Modules

Creating a module in Java requires quite a bit of extra boilerplate.

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:

fun main(vararg args: String) {
    val myModule = module {
        bind(SomeService::class).to(SomeServiceImpl::class)
        // Or, even simpler with reified generics
        bind<SomeService>().to<SomeServiceImpl>()
    }
    val injector = Guice.createInjector(myModule)
}

The library also defines a simple way of declaring private modules:

fun main(vararg args: String) {
    val privateModule = privateModule {
        bind<SomeService>().to<SomeServiceImpl>()
        expose<SomeService>()
    }
    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.