Git Product home page Git Product logo

kotlin-guiced's Introduction

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.

kotlin-guiced's People

Contributors

jlleitschuh avatar octogonapus avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

kotlin-guiced's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.