Dependency Injection in Scala - Cake Pattern

4,024 views
3,727 views

Published on

Second part of the presentation about dependency injection in Scala from January's meetup of the Czech Scala Enthusiasts.
This part addresses the Cake Pattern - Scala's direct language support for DI.

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

No Downloads
Views
Total views
4,024
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
0
Comments
0
Likes
14
Embeds 0
No embeds

No notes for slide

Dependency Injection in Scala - Cake Pattern

  1. 1. Scala Cake Pattern The dependency non-injection un-frameworkúterý, 29. ledna 13
  2. 2. The cake is a lie • No injection • No framework • Just Scala language featuresúterý, 29. ledna 13
  3. 3. The patternúterý, 29. ledna 13
  4. 4. The top level object DeluxeEspressoMachine extends EspressoMachine with FineGrinderComponent with AutomaticFoamerComponent with FastWaterHeaterComponent object BudgetEspressoMachine extends EspressoMachine with RoughGrinderComponent with ManualFoamerComponent with SlowWaterHeaterComponentúterý, 29. ledna 13
  5. 5. The top level object DeluxeEspressoMachine extends EspressoMachine with FineGrinderComponent with AutomaticFoamerComponent with FastWaterHeaterComponentúterý, 29. ledna 13
  6. 6. The top level object DeluxeEspressoMachine extends EspressoMachine with FineGrinderComponent with AutomaticFoamerComponent with FastWaterHeaterComponentúterý, 29. ledna 13
  7. 7. The Espresso Machine trait EspressoMachine { self: WaterHeaterComponent with GrinderComponent with FoamerComponent => def makeCoffee = { val beans = grinder.grind val water = heater.heatWater val foam = foamer.makeFoam ... } }úterý, 29. ledna 13
  8. 8. The Espresso Machine trait EspressoMachine { self: WaterHeaterComponent with GrinderComponent with FoamerComponent => def makeCoffee = { val beans = grinder.grind val water = heater.heatWater val foam = foamer.makeFoam ... } }úterý, 29. ledna 13
  9. 9. The Espresso Machine trait EspressoMachine { self: WaterHeaterComponent with GrinderComponent with FoamerComponent => def makeCoffee = { val beans = grinder.grind val water = heater.heatWater val foam = foamer.makeFoam ... } }úterý, 29. ledna 13
  10. 10. The self-typeúterý, 29. ledna 13
  11. 11. The Component trait GrinderComponent { def grinder: Grinder trait Grinder { def grind: GroundBeans } }úterý, 29. ledna 13
  12. 12. The Implementation trait FineGrinderComponent extends GrinderComponent { val grinder = new FineGrinder class FineGrinder extends Grinder { def grind = GroundBeans(GrindFineness.Fine) } }úterý, 29. ledna 13
  13. 13. The top level object DeluxeEspressoMachine extends EspressoMachine with FineGrinderComponent with AutomaticFoamerComponent with FastWaterHeaterComponentúterý, 29. ledna 13
  14. 14. Pro/Cons • Pros • Comes with the language • Statically typed & immutable • Compiler helps a lot • Cons • No XML and annotationsúterý, 29. ledna 13
  15. 15. The language features • Trait • Self-type • Singletonúterý, 29. ledna 13
  16. 16. References • http://www.cakesolutions.net/teamblogs/ 2011/12/19/cake-pattern-in-depth/ • http://jonasboner.com/2008/10/06/real- world-scala-dependency-injection-di/ • http://www.warski.org/blog/2010/12/di-in- scala-cake-pattern/ • http://lamp.epfl.ch/~odersky/papers/ ScalableComponent.pdfúterý, 29. ledna 13

×