Giter Site home page Giter Site logo

sse-guice's Introduction

sse-guice

A library to make the Guice internal DSL more Scala friendly.

The motivation for this library is the fact that, although Scala provides lots of syntactic ways to create internal DSLs, when working with the Guice internal DSL the Java code is both sorter an more readable. The main reason for this is the absence of class literals in Scala. Instead of writing Service.class in Scala we write classOf[Service] which is longer and less readable.

To address this problem sse-guice extends the interfaces of Guice's internal DSL (defined in com.google.inject.binder) by adding a method that takes a Manifest for each method that takes Class as a parameter. This allows us to write:

bind[Service].to[ServiceImpl].in[Singleton]

instead of:

bind(classOf[Service]).to(classOf[ServiceImpl]).in(classOf[Singleton])

which is what we have to write when using Guice in Scala.

An added benefit of Manifests is that they not only allow us to get rid of classes but also TypeLiterals. To define a binding for a generic type in Guice (because of erasure) we have to construct a TypeLiteral. For example to bind Validator[Registration] to RegistrationVSpec we have to write:

bind(new TypeLiteral[Validator[Registration]] {}).to(classOf[RegistrationVSpec])

Because Manifest also has information about the type arguments of a type when using the methods that take Manifest as an argument sse-guice is able to detect if the type has type arguments and construct a TypeLiteral automatically. This allows us to write:

  bind[Validator[Registration]].to[RegistrationVSpec]

and avoid the explicit creation of the TypeLiteral.

Usage

To use this library your Guice modules must extend the ScalaModule abstract class instead of the AbstractModule. The ScalaModule adds methods and overrides any methods that return binder interfaces to return the enhanced interfaces of this library.

class MyModule extends ScalaModule {
  def configure() {
    bind[Service].to[ServiceImpl].in[Singleton]
	bind[CreditCardPaymentService]
	bind[PaymentService].to[CreditCardPaymentService]
    bindConstant().annotatedWithName("port").to(8080)
  }
} 

License

Licensed under the Apache License, Version 2.0. See the LICENSE and NOTICE files for more information.

sse-guice's People

Contributors

andrewconner avatar sptz45 avatar

Watchers

 avatar  avatar

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.