BeScala -  Scala Guice
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

BeScala - Scala Guice

  • 3,760 views
Uploaded on

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

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

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

More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,760
On Slideshare
3,741
From Embeds
19
Number of Embeds
6

Actions

Shares
Downloads
19
Comments
0
Likes
5

Embeds 19

http://paper.li 11
https://twitter.com 3
http://dschool.co 2
http://twitter.com 1
http://tweetedtimes.com 1
http://trunk.ly 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. Scala Guice
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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!