SlideShare una empresa de Scribd logo
1 de 13
Scala Guice
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])
         }
     }
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);
         }
     };
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]
         }
     }
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...
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[]
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);
    }
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.
Using Scala to express what you really want
I 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]
  }
Using Scala to express what you really want
I 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
Using Scala to express what you really want
I 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



When 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()) }
  }
Scala Guice - internals
 trait ConfigurationBuilder {
     def config(config: => Unit): Configuration = { ... }
     def bind[T](implicit manifT: Manifest[T]): ScalaAnnotatedBindingBuilder[T] = { ... }
 }

 object ConfigurationBuilder extends ConfigurationBuilder




Scala 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
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!

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

"Inside The AngularJS Directive Compiler" by Tero Parviainen
"Inside The AngularJS Directive Compiler" by Tero Parviainen"Inside The AngularJS Directive Compiler" by Tero Parviainen
"Inside The AngularJS Directive Compiler" by Tero Parviainen
 
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
#36.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_재직자환급교육,실업자교육,국비지원교육, 자바교육,구...
 
Typescript barcelona
Typescript barcelonaTypescript barcelona
Typescript barcelona
 
AngularJs-training
AngularJs-trainingAngularJs-training
AngularJs-training
 
(국비지원학원/재직자교육/실업자교육/IT실무교육_탑크리에듀)#4.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
(국비지원학원/재직자교육/실업자교육/IT실무교육_탑크리에듀)#4.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)(국비지원학원/재직자교육/실업자교육/IT실무교육_탑크리에듀)#4.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
(국비지원학원/재직자교육/실업자교육/IT실무교육_탑크리에듀)#4.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)
 
Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3 Intro to Redux | DreamLab Academy #3
Intro to Redux | DreamLab Academy #3
 
Firebase ng2 zurich
Firebase ng2 zurichFirebase ng2 zurich
Firebase ng2 zurich
 
Angular2 + rxjs
Angular2 + rxjsAngular2 + rxjs
Angular2 + rxjs
 
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
#18.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
 
Java script advance-auroskills (2)
Java script advance-auroskills (2)Java script advance-auroskills (2)
Java script advance-auroskills (2)
 
Demystifying Oak Search
Demystifying Oak SearchDemystifying Oak Search
Demystifying Oak Search
 
Workshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & ReduxWorkshop 20: ReactJS Part II Flux Pattern & Redux
Workshop 20: ReactJS Part II Flux Pattern & Redux
 
G pars
G parsG pars
G pars
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
Build Widgets
Build WidgetsBuild Widgets
Build Widgets
 
Rxjs marble-testing
Rxjs marble-testingRxjs marble-testing
Rxjs marble-testing
 
Workshop 26: React Native - The Native Side
Workshop 26: React Native - The Native SideWorkshop 26: React Native - The Native Side
Workshop 26: React Native - The Native Side
 
Workshop 3: JavaScript build tools
Workshop 3: JavaScript build toolsWorkshop 3: JavaScript build tools
Workshop 3: JavaScript build tools
 
Implement react pagination with react hooks and react paginate
Implement react pagination with react hooks and react paginateImplement react pagination with react hooks and react paginate
Implement react pagination with react hooks and react paginate
 
Workshop 17: EmberJS parte II
Workshop 17: EmberJS parte IIWorkshop 17: EmberJS parte II
Workshop 17: EmberJS parte II
 

Destacado

Google Guice presentation at Montreal JUG - October 2010
Google Guice presentation at Montreal JUG - October 2010Google Guice presentation at Montreal JUG - October 2010
Google Guice presentation at Montreal JUG - October 2010
Mathieu Carbou
 
Ozma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrencyOzma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrency
BeScala
 

Destacado (9)

Google Guice presentation at Montreal JUG - October 2010
Google Guice presentation at Montreal JUG - October 2010Google Guice presentation at Montreal JUG - October 2010
Google Guice presentation at Montreal JUG - October 2010
 
Clean code via dependency injection + guice
Clean code via dependency injection + guiceClean code via dependency injection + guice
Clean code via dependency injection + guice
 
Ozma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrencyOzma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrency
 
ScalaCheck Cookbook v1.0
ScalaCheck Cookbook v1.0ScalaCheck Cookbook v1.0
ScalaCheck Cookbook v1.0
 
Google Guice
Google GuiceGoogle Guice
Google Guice
 
Guice - dependency injection framework
Guice - dependency injection frameworkGuice - dependency injection framework
Guice - dependency injection framework
 
Eway google-guice presentation
Eway google-guice presentationEway google-guice presentation
Eway google-guice presentation
 
Introduction to Google Guice
Introduction to Google GuiceIntroduction to Google Guice
Introduction to Google Guice
 
5-minute intro to property-based testing in Python with hypothesis
5-minute intro to property-based testing in Python with hypothesis5-minute intro to property-based testing in Python with hypothesis
5-minute intro to property-based testing in Python with hypothesis
 

Similar a BeScala - Scala Guice

Jug Guice Presentation
Jug Guice PresentationJug Guice Presentation
Jug Guice Presentation
Dmitry Buzdin
 

Similar a BeScala - Scala Guice (20)

Dsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in ScalaDsug 05 02-15 - ScalDI - lightweight DI in Scala
Dsug 05 02-15 - ScalDI - lightweight DI in Scala
 
Web Components Everywhere
Web Components EverywhereWeb Components Everywhere
Web Components Everywhere
 
Solid angular
Solid angularSolid angular
Solid angular
 
Jug Guice Presentation
Jug Guice PresentationJug Guice Presentation
Jug Guice Presentation
 
Liferay (DXP) 7 Tech Meetup for Developers
Liferay (DXP) 7 Tech Meetup for DevelopersLiferay (DXP) 7 Tech Meetup for Developers
Liferay (DXP) 7 Tech Meetup for Developers
 
The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210The Ring programming language version 1.9 book - Part 99 of 210
The Ring programming language version 1.9 book - Part 99 of 210
 
Learning .NET Attributes
Learning .NET AttributesLearning .NET Attributes
Learning .NET Attributes
 
Learn dot net attributes
Learn dot net attributesLearn dot net attributes
Learn dot net attributes
 
Building Reusable Custom Elements With Angular
Building Reusable Custom Elements With AngularBuilding Reusable Custom Elements With Angular
Building Reusable Custom Elements With Angular
 
ARGUS - THE OMNISCIENT CI
ARGUS - THE OMNISCIENT CIARGUS - THE OMNISCIENT CI
ARGUS - THE OMNISCIENT CI
 
Backbone.js
Backbone.jsBackbone.js
Backbone.js
 
Angular js
Angular jsAngular js
Angular js
 
Cloudformation101
Cloudformation101Cloudformation101
Cloudformation101
 
Spock's New Tricks
Spock's New TricksSpock's New Tricks
Spock's New Tricks
 
Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018
 
Learn about dot net attributes
Learn about dot net attributesLearn about dot net attributes
Learn about dot net attributes
 
The Ring programming language version 1.7 book - Part 88 of 196
The Ring programming language version 1.7 book - Part 88 of 196The Ring programming language version 1.7 book - Part 88 of 196
The Ring programming language version 1.7 book - Part 88 of 196
 
Custom AngularJS Directives
Custom AngularJS DirectivesCustom AngularJS Directives
Custom AngularJS Directives
 
AngularJS Custom Directives
AngularJS Custom DirectivesAngularJS Custom Directives
AngularJS Custom Directives
 
clodfoundrydoc.pdf
clodfoundrydoc.pdfclodfoundrydoc.pdf
clodfoundrydoc.pdf
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

BeScala - 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 want I 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 want I 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 want I 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 When 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 ConfigurationBuilder Scala 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!

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n