Dependency Injection in Scala, part 1

7,440 views
7,122 views

Published on

First part of the presentation about dependency injection in Scala from January's meetup of the Czech Scala Enthusiasts.
This part addresses usage of Java DI frameworks in Scala (CDI, Spring, Guice) and SubCut (pure Scala library).

Published in: Technology
0 Comments
11 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,440
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
11
Embeds 0
No embeds

No notes for slide

Dependency Injection in Scala, part 1

  1. 1. Dependency Injection in Scala Vlastimil Menčík, Jakub Janeček, Michal Příhoda Czech Scala Enthusiasts
  2. 2. 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
  3. 3. 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
  4. 4. The coffee-making domain
  5. 5. Spring (annotations)
  6. 6. Spring (the same in Java)
  7. 7. Spring (application context)
  8. 8. Spring observations● actual differences between Java and Scala were minimal● constructor injection works well with immutability● setter injection requires some additional work
  9. 9. Setter injection in Java
  10. 10. Setter injection in Scala
  11. 11. 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
  12. 12. Spring Scala (no annotations)
  13. 13. Spring Scala (wiring)
  14. 14. Spring Scala (application context)
  15. 15. 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
  16. 16. Vanilla Guice
  17. 17. Guice with Scala wrapper
  18. 18. Guice Observations● simplicity● interesting if you dont need (or want) Spring● easy to integrate with other frameworks (even the Scala ones)
  19. 19. 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
  20. 20. CDI annotation(s)
  21. 21. CDI (beans.xml)
  22. 22. Scopes and Contexts● singleton● prototype (aka dependent)● session● request● application● ...
  23. 23. 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
  24. 24. Proxies● restrictions on the proxied class ○ might vary between DI frameworks● needs a default constructor● cannot be final or have final methods
  25. 25. 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"
  26. 26. Java DI Frameworks Summary● you can use them if you want ○ Spring is not just DI
  27. 27. Java DI Frameworks Summary● maybe with some additional wrappers to make them more Scala-friendly
  28. 28. Java DI Frameworks Summary● problems may arise if there are additional restrictions on classes (setters, proxies, ...) ○ these can be avoided with some effort
  29. 29. 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
  30. 30. Step 1: Define Your Components
  31. 31. Step 2: Wireem All
  32. 32. Step 3: Define Your Injectable Class
  33. 33. Step 4: Run or Test
  34. 34. Pros&Cons● Pros ○ straightforward and simple ○ idiomatic● Cons ○ no automatic injection ○ possible runtime errors (not static as Cake)

×