Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency injection in Scala: The sequel - SubCut

Dependency injection in Scala: The sequel - SubCut

A follow up presentation on dependency injection in Scala using SubCut.

Jan Fredrik Wedén

December 01, 2011
Tweet

More Decks by Jan Fredrik Wedén

Other Decks in Programming

Transcript

  1. History  lesson   SubCut   (My  personal)  Crystal  Ball  

    Scala  Uniquely  Bound  Classes  Under  Traits  
  2. trait SomeComponent { val aThing: SomeInterface trait SomeInterface { //

    Stuff } } trait AnotherComponentImpl extends AnotherComponent { this: SomeComponent => def myMethod { aThing.usefulStuff() } }   object ComponentRegistry extends AnotherComponentImpl with SomeComponentImpl { val aThing = new MyFavouriteImpl }
  3. object ProjectConfiguration extends NewBindingModule({ module => import module._ // can

    now use bind directly bind [UserRepository] toInstance new RealUserRepository bind [Analyzer] identifiedBy 'webAnalyzer to instanceOfClass [WebAnalyzer] bind [Session] identifiedBy 'currentUser toProvider { WebServerSession.getCurrentUser().getSession() } bind [Int] identifiedBy 'maxThreadPoolSize toInstance 10 bind [WebSearch] toLazyInstance { new GoogleSearchService()(ProjectConfiguration) } ... }) Configura+on  module  
  4. Injectable  class   object ProjectConfiguration extends NewBindingModule({ module => ...

    }) class UserService(val serviceId: String, webAnalyzer: Option[Analyzer] = injected) (implicit val bindingModule: BindingModule) extends Injectable { val userRepository = inject[UserRepository] val maxPoolSize = injectIfBound[Int]('maxThreadPoolSize) { 15 } val myAnalyzer = injectIfMissing[Analyzer](webAnalyzer) ... }
  5. Configuring  your  app   implicit val bindingModule = ProjectConfiguration val

    topInstance = new UserService("myUserService") val topInstance = new UserService("myUserService")(ProjectConfiguration)
  6. Configuring  your  app   implicit val bindingModule = ProjectConfiguration val

    topInstance = new UserService("myUserService") val topInstance = new UserService("myUserService")(ProjectConfiguration) trait ProjectBindings { implicit val bindingModule: BindingModule = ProjectConfiguration } class MyApp extends Injectable with ProjectBindings { // ... }
  7. Configuring  your  app   implicit val bindingModule = ProjectConfiguration val

    topInstance = new UserService("myUserService") val topInstance = new UserService("myUserService")(ProjectConfiguration) class SomePage(implicit val bindingModule: BindingModule) extends WicketPage with Injectable { } class ProdSomePage extends SomePage(ProjectConfiguration) trait ProjectBindings { implicit val bindingModule: BindingModule = ProjectConfiguration } class MyApp extends Injectable with ProjectBindings { // ... }
  8. Tes+ng   test(”SubCut scalatest test 1") { ProjectConfiguration.modifyBindings { module

    => // new calls to bind inside here val service = new UserService("myUserService")(module) // all testcode using service also goes inside here } }
  9. Tes+ng   val testConfig = ProjectConfiguration.modifyBindings { module => //

    new calls to bind inside here module } val service = new UserService(“myOtherService”)(testConfig) test(”SubCut scalatest test 2") { // testcode using service goes here } test(”SubCut scalatest test 1") { ProjectConfiguration.modifyBindings { module => // new calls to bind inside here val service = new UserService("myUserService")(module) // all testcode using service also goes inside here } }
  10. Tes+ng   val testModule = TestProjectConfiguration :: ProjectConfiguration val mergedService

    = new UserService("myMergedService")(testModule) test(”SubCut scalatest test 3") { // testcode using mergedService goes here } val testConfig = ProjectConfiguration.modifyBindings { module => // new calls to bind inside here module } val service = new UserService(“myOtherService”)(testConfig) test(”SubCut scalatest test 2") { // testcode using service goes here } test(”SubCut scalatest test 1") { ProjectConfiguration.modifyBindings { module => // new calls to bind inside here val service = new UserService("myUserService")(module) // all testcode using service also goes inside here } }
  11. Credits   •  “Dependency  Injec+on  Op+ons  in  Scala”  –  Dick

      Wall  @  JavaZone  X  2011   – h?p://vimeo.com/28792660   •  h?ps://github.com/dickwall/subcut   •  h?ps://github.com/dickwall/subcut/blob/ master/Ge`ngStarted.md   •  h?p://stackoverflow.com/ques+ons/7029268/ use-­‐subcut-­‐scala-­‐di-­‐on-­‐android   •  h?ps://github.com/janwe/javaBin-­‐ScalaDI  
  12. BEKK CONSULTING AS SKUR 39, VIPPETANGEN. P.O. BOX 134 SENTRUM,

    0102 OSLO, NORWAY. WWW.BEKK.NO Jan  Fredrik  Wedén   Manager,  Trondheim   @jfweden