Your SlideShare is downloading. ×
0
Scala Guice
Scala Guice - Motivation  Google Guice with Scala is too verbose.     val module = new AbstractModule() {         override...
Scala Guice - Motivation  Google Guice with Scala is too verbose.     val module = new AbstractModule() {         override...
Scala Guice - Motivation  We can do it better if we use    scala.reflect.Manifest[T]     val module = new ScalaModule() { ...
Scala Guice - Motivation  We can do it better if we use      scala.reflect.Manifest[T]       val module = new ScalaModule(...
Scala Guice - Motivation  We can do it better if we use      scala.reflect.Manifest[T]       val module = new ScalaModule(...
Google Guice - internals    Module module = new AbstractModule() {        @Override        protected void configure() {   ...
Google Guice - internals    Module module = new AbstractModule() {        @Override        protected void configure() {   ...
Using Scala to express what you really wantI want to have my binding configuration independent of the binding process.  val...
Using Scala to express what you really wantI want to have my binding configuration independent of the binding process.  val...
Using Scala to express what you really wantI want to have my binding configuration independent of the binding process.  val...
Scala Guice - internals trait ConfigurationBuilder {     def config(config: => Unit): Configuration = { ... }     def bind...
Source code https://github.com/rcavalcanti/scala-guice  • No production code  • Not extensively tested  • Not covering the...
Upcoming SlideShare
Loading in...5
×

BeScala - Scala Guice

3,098

Published on

Presentation about Scala Guice, a thin Scala layer around Google Guice API.

Presented on September 13th, at BeScala Meet-up.

Published in: Technology, Business
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,098
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
22
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Transcript of "BeScala - Scala Guice"

    1. 1. Scala Guice
    2. 2. Scala Guice - Motivation Google Guice with Scala is too verbose. val module = new AbstractModule() { override def configure() { bind(classOf[Service]).to(classOf[ServiceImpl]) bind(classOf[BaseDao]).to(classOf[BaseDaoImpl]) } }
    3. 3. Scala Guice - Motivation Google Guice with Scala is too verbose. val module = new AbstractModule() { override def configure() { bind(classOf[Service]).to(classOf[ServiceImpl]) bind(classOf[BaseDao]).to(classOf[BaseDaoImpl]) } } You write less code using Java. Module module = new AbstractModule() { @Override protected void configure() { bind(Service.class).to(ServiceImpl.class); bind(BaseDao.class).to(BaseDaoImpl.class); } };
    4. 4. Scala Guice - Motivation We can do it better if we use scala.reflect.Manifest[T] val module = new ScalaModule() { override def configure() { bind[Service].to[ServiceImpl] bind[BaseDao].to[BaseDaoImpl] } }
    5. 5. Scala Guice - Motivation We can do it better if we use scala.reflect.Manifest[T] val module = new ScalaModule() { override def configure() { bind[Service].to[ServiceImpl] bind[BaseDao].to[BaseDaoImpl] } } • But we’ll have to mimic the whole Guice API...
    6. 6. Scala Guice - Motivation We can do it better if we use scala.reflect.Manifest[T] val module = new ScalaModule() { override def configure() { bind[Service].to[ServiceImpl] bind[BaseDao].to[BaseDaoImpl] } } • But we’ll have to mimic the whole Guice API... • Only to remove classOf[]
    7. 7. Google Guice - internals Module module = new AbstractModule() { @Override protected void configure() { bind(Service.class).to(ServiceImpl.class); bind(BaseDao.class).to(BaseDaoImpl.class); } }; protected <T> AnnotatedBindingBuilder<T> bind(Class<T>clazz) { return binder.bind(clazz); }
    8. 8. Google Guice - internals Module module = new AbstractModule() { @Override protected void configure() { bind(Service.class).to(ServiceImpl.class); bind(BaseDao.class).to(BaseDaoImpl.class); } }; protected <T> AnnotatedBindingBuilder<T> bind(Class<T>clazz) { return binder.bind(clazz); } The binding starts as soon as method configure is called. The wiring is no dissociated from its definition.
    9. 9. Using Scala to express what you really wantI want to have my binding configuration independent of the binding process. val services : Configuration = config { bind[Service].to[ServiceImpl] } val daos : Configuration = config { bind[BaseDao].to[BaseDaoImpl] }
    10. 10. Using Scala to express what you really wantI want to have my binding configuration independent of the binding process. val services : Configuration = config { bind[Service].to[ServiceImpl] } val daos : Configuration = config { bind[BaseDao].to[BaseDaoImpl] }I want to merge my configuration, without starting the binding. val mergedConfig = services ++ daos
    11. 11. Using Scala to express what you really wantI want to have my binding configuration independent of the binding process. val services : Configuration = config { bind[Service].to[ServiceImpl] } val daos : Configuration = config { bind[BaseDao].to[BaseDaoImpl] }I want to merge my configuration, without starting the binding. val mergedConfig = services ++ daosWhen I’m ready, I can pass my Configuration to a ScalaModule class ScalaModule(val configurations: Configuration*) extends AbstractModule { def configure() = configurations.foreach { c => c.bind(binder()) } }
    12. 12. Scala Guice - internals trait ConfigurationBuilder { def config(config: => Unit): Configuration = { ... } def bind[T](implicit manifT: Manifest[T]): ScalaAnnotatedBindingBuilder[T] = { ... } } object ConfigurationBuilder extends ConfigurationBuilderScala Guice Builders - mimic Guice API• ScalaScopedBindingBuilder• ScalaLinkedBindingBuilder• ScalaAnnotatedBindingBuilder def to(impl: Class[_ <: T]): ScalaScopedBindingBuilder = { // delay call to builder linkedCommands += { (builder: LinkedBindingBuilder[T]) => builder.to(impl) } this } But real Guice call is recorded for later use Command pattern with Closures
    13. 13. Source code https://github.com/rcavalcanti/scala-guice • No production code • Not extensively tested • Not covering the whole Guice API Feedback and contributions are welcome!
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×