sse-guice

Make the Guice EDSL more Scala friendly

License

License

Categories

Categories

GUI User Interface Guice Application Layer Libs Dependency Injection
GroupId

GroupId

com.tzavellas
ArtifactId

ArtifactId

sse-guice
Last Version

Last Version

0.7.1
Release Date

Release Date

Type

Type

jar
Description

Description

sse-guice
Make the Guice EDSL more Scala friendly
Project Organization

Project Organization

spiros.blog()
Source Code Management

Source Code Management

http://github.com/sptz45/sse-guice/tree/master

Download sse-guice

How to add to project

<!-- https://jarcasting.com/artifacts/com.tzavellas/sse-guice/ -->
<dependency>
    <groupId>com.tzavellas</groupId>
    <artifactId>sse-guice</artifactId>
    <version>0.7.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.tzavellas/sse-guice/
implementation 'com.tzavellas:sse-guice:0.7.1'
// https://jarcasting.com/artifacts/com.tzavellas/sse-guice/
implementation ("com.tzavellas:sse-guice:0.7.1")
'com.tzavellas:sse-guice:jar:0.7.1'
<dependency org="com.tzavellas" name="sse-guice" rev="0.7.1">
  <artifact name="sse-guice" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.tzavellas', module='sse-guice', version='0.7.1')
)
libraryDependencies += "com.tzavellas" % "sse-guice" % "0.7.1"
[com.tzavellas/sse-guice "0.7.1"]

Dependencies

compile (2)

Group / Artifact Type Version
org.scala-lang : scala-library jar 2.10.0
com.google.inject : guice jar 3.0

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

sse-guice

Build Status

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.

Setup

For Scala 2.9.x use:

"com.tzavellas" % "sse-guice" % "0.6.1"

For Scala 2.10.x and 2.11.x use:

"com.tzavellas" %% "sse-guice" % "0.7.2"

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[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.

Versions

Version
0.7.1
0.7.0
0.6.1