Slide 1

Slide 1 text

Dependency Injection in Scala Vlastimil Menčík, Jakub Janeček, Michal Příhoda Czech Scala Enthusiasts

Slide 2

Slide 2 text

Java DI frameworks with Scala ● important for existing Java-based projects/platforms ● Java-Scala interoperability on JVM-level bears fruits ● some issues may arise ○ we will show some later

Slide 3

Slide 3 text

Spring ● most popular ● well-designed ● a lot more than just an IoC container ● a lot of people have experimented with Spring + Scala ● SpringSource is working on the Spring Scala project

Slide 4

Slide 4 text

The coffee-making domain

Slide 5

Slide 5 text

Spring (annotations)

Slide 6

Slide 6 text

Spring (the same in Java)

Slide 7

Slide 7 text

Spring (application context)

Slide 8

Slide 8 text

Spring observations ● actual differences between Java and Scala were minimal ● constructor injection works well with immutability ● setter injection requires some additional work

Slide 9

Slide 9 text

Setter injection in Java

Slide 10

Slide 10 text

Setter injection in Scala

Slide 11

Slide 11 text

Spring Scala project ● developed by SpringSource ● targets Spring 3.2 ● work in progres (1.0.0.M1) ● wiring beans using functional syntax ● functional wrappers for Spring templates ○ functions instead of anonymous callbacks

Slide 12

Slide 12 text

Spring Scala (no annotations)

Slide 13

Slide 13 text

Spring Scala (wiring)

Slide 14

Slide 14 text

Spring Scala (application context)

Slide 15

Slide 15 text

Google Guice ● focuses only on the DI ● leaner, easier to get into ● less magic ● explicit bindings ○ defined in module classes ● just-in-time bindings ○ automatic discovery

Slide 16

Slide 16 text

Vanilla Guice

Slide 17

Slide 17 text

Guice with Scala wrapper

Slide 18

Slide 18 text

Guice Observations ● simplicity ● interesting if you don't need (or want) Spring ● easy to integrate with other frameworks (even the Scala ones)

Slide 19

Slide 19 text

Contexts and Dependency Injection ● the standard (JSR-299) ● part of Java EE 6 (and every compliant container) ● created with lessons from previous DI solutions in mind

Slide 20

Slide 20 text

CDI annotation(s)

Slide 21

Slide 21 text

CDI (beans.xml)

Slide 22

Slide 22 text

Scopes and Contexts ● singleton ● prototype (aka dependent) ● session ● request ● application ● ...

Slide 23

Slide 23 text

Scopes cont. ● in CDI, only singleton and dependent are injected as direct references ○ different lifecycles, serialization ● DI framework will inject a proxy instead ○ thread-local magic

Slide 24

Slide 24 text

Proxies ● restrictions on the proxied class ○ might vary between DI frameworks ● needs a default constructor ● cannot be final or have final methods

Slide 25

Slide 25 text

Proxy problems ● Scala compiler emits final methods ○ even if there are none in the source code ● access to a private val from a closure ● such class is "unproxyable"

Slide 26

Slide 26 text

Java DI Frameworks Summary ● you can use them if you want ○ Spring is not just DI

Slide 27

Slide 27 text

Java DI Frameworks Summary ● maybe with some additional wrappers to make them more Scala-friendly

Slide 28

Slide 28 text

Java DI Frameworks Summary ● problems may arise if there are additional restrictions on classes (setters, proxies, ...) ○ these can be avoided with some effort

Slide 29

Slide 29 text

SubCut "Scala Uniquely Bound Classes Under Traits" https://github.com/dickwall/subcut ● very small and simple DI library ● pure and idiomatic Scala ● binding DSL ● uses implicits to cut down on boilerplate JavaPosse ScalaWags

Slide 30

Slide 30 text

Step 1: Define Your Components

Slide 31

Slide 31 text

Step 2: Wire'em All

Slide 32

Slide 32 text

Step 3: Define Your Injectable Class

Slide 33

Slide 33 text

Step 4: Run or Test

Slide 34

Slide 34 text

Pros&Cons ● Pros ○ straightforward and simple ○ idiomatic ● Cons ○ no automatic injection ○ possible runtime errors (not static as Cake)