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

Dependency Injection in Scala

E4b90a83a8579205f3b18c9855062526?s=47 Yuki Ishikawa
December 06, 2017

Dependency Injection in Scala

青山エンジニア勉強交流会~Scala勉強会~ (2017/12/04)
「DIとは何か」について軽く説明した後、ScalaのDI手法を4つ(Manual DI, Cake Pattern, MacWire, Guice)紹介しました

E4b90a83a8579205f3b18c9855062526?s=128

Yuki Ishikawa

December 06, 2017
Tweet

Transcript

  1. Dependency Injection in Scala גࣜձࣾϑΝϯίϛϡχέʔγϣϯζ ੴ઒ ༟थ

  2. Profile • ϑΝϯίϛೖࣾ4೥໨ • Scalaྺ2೥൒͘Β͍ (લ: Java, Ruby, Perl,PHP, etc.)

    • nend, nex8, viidleͷ഑৴γεςϜߏஙΛ୲౰ • ࠷ۙ͸ϒϩοΫνΣʔϯͷษڧΛͨ͠Γ ੴ઒ ༟थ Yuki Ishikawa
  3. Dependency Injection (DI) ͱ͸Կ͔ʁ

  4. Dependency Injection (DI) ͱ͸Կ͔ʁ ίϯϙʔωϯτؒͷґଘؔ܎ΛϓϩάϥϜͷ ιʔείʔυ͔Βഉআ͠ɺ֎෦ͷઃఆϑΝΠ ϧͳͲͰ஫ೖͰ͖ΔΑ͏ʹ͢Διϑτ΢ΣΞ ύλʔϯͰ͋Δɻ WikipediaΑΓ https://ja.wikipedia.org/wiki/ґଘੑͷ஫ೖ

  5. "E$POUSPMMFS "E4FSWJDF "E4FSWJDF*NQM Dependency Injection (DI) ͱ͸Կ͔ʁ .PEVMF *OKFDUPS AdController:

    ޿ࠂϦΫΤετͷϋϯυϦϯά AdService: ࠷దͳ޿ࠂͷબग़ϩδοΫ
  6. Dependency Injection (DI) ͱ͸Կ͔ʁ Manual DIʹΑΔ࣮૷ྫɻDIͷํ๏͸Կछྨ΋ଘࡏ͢Δ

  7. Dependency Injection (DI) ͱ͸Կ͔ʁ Manual DIʹΑΔ࣮૷ྫɻDIͷํ๏͸Կछྨ΋ଘࡏ͢Δ wiring(഑ઢ)

  8. Կ͕خ͍͠ͷ?

  9. Կ͕خ͍͠ͷʁ ‣ ݁߹౓ͷ௿ԼɾίϯϙʔωϯτԽͷଅਐ ‣ ୯ମςετ͕͠΍͘͢ͳΔ

  10. ScalaͰDI͢Δํ๏

  11. • MacWire • Guice • Spring • Scaldi • SubCut

    • Freestyle
 (Free Monad & Taggless Final) Pure Scala Library • Manual DI • Cake Pattern • Reader Monad • Free Monad • Tagless Final ScalaͷDIύλʔϯ
  12. ͕࣌ؒ͋·Γແ͍ͷͰ • Manual DI • Cake Pattern • MacWire •

    Guice ͷ̐छྨΛ঺հ͠·͢ʂ
  13. Manual DI (Constructor Injection) • ίϯετϥΫλҾ਺Λ࢖ͬͯख࡞ۀͰ഑ઢ • Ұ൪؆୯ʹDI͢Δํ๏

  14. Manual DI (Constructor Injection) ख࡞ۀͰ഑ઢ͢Δ

  15. Manual DI (Constructor Injection) ςετ͢Δͱ͖͸ܧঝͯ͠mock/stubͰ্ॻ͖͢Ε͹OK

  16. Manual DI (Constructor Injection) Pros Cons • Ұ൪खܰʹ࢝ΊΒΕΔํ๏ • Compile-time

    DI • Pure Scala • ґଘؔ܎͕ଟ͍ͱ഑ઢ࡞ۀ͕ͭΒ͍ খن໛ͷϓϩδΣΫτͳΒManualͰे෼!!
  17. Cake Pattern • ίϯετϥΫλҾ਺Λ࢖༻͠ͳ͍໘ന͍Ξϓϩʔν ‣ ࣗ෼ܕΞϊςʔγϣϯ΍ܧঝͰDI͢Δ • nendͰ࠾༻͍ͯ͠Δ • આ໌͕೉͍͠

  18. Cake Pattern ίϯϙʔωϯτΛ࡞੒

  19. Cake Pattern MixinͰΦϒδΣΫτΛ஫ೖ͢Δ

  20. Cake Pattern Minimal Cake Pattern υϫϯΰΞΧ΢ϯτγεςϜͰ࠾༻͞Ε͍ͯΔख๏ ʢࣗ෼ܕΞϊςʔγϣϯ͕ొ৔͠ͳ͍ͷͰཧղ͠΍͍͢ʣ https://qiita.com/pab_tech/items/1c0bdbc8a61949891f1f

  21. Cake Pattern Pros Cons • Compile-time DI • Pure Scala

    • େྔͷϘΠϥʔϓϨʔτ • ॊೈੑ͕௿͍ ‣ Component͕ΠϯελϯεΛ͔̍ͭ࣋ͯ͠ͳ͍ͷͰɺ2ͭͷಉ͡ ܕʹґଘͰ͖ͳ͍ • ύϑΥʔϚϯεɾίϯύΠϧ࣌ؒʹѱӨڹʁ(ະݕূ)
  22. MacWire • Scala༻ϥΠϒϥϦ • ϚΫϩͰ഑ઢͯ͘͠ΕΔ • viidleͰ࠾༻͍ͯ͠Δ • Adam Warski͞Μ͕࡞ͬͯΔ

    ‣ ϒϩάͰScalaͷDIؔ࿈ͷهࣄΛެ։ͯ͠Δ ‣ ࠷ۙ͸sttpͱ͔࡞ͬͯΔ
  23. MacWire wire͢Δ͚ͩͰɺ͋ͱ͸ϚΫϩ͕΍ͬͯ͘ΕΔ

  24. MacWire wireSetͰෳ਺ΦϒδΣΫτͷSetԽ͕Մೳ

  25. MacWire scala-commonͰλά෇͚͕Մೳ (λά෇͚ͨ͘͠ͳ͍৔߹͸Manual DIͱซ༻ͯ͠΋ྑ͍)

  26. MacWire Pros Cons • ϘΠϥʔϓϨʔτ͕গͳ͍ • Compile-time DI • ϚΫϩ

    (ڧ͍ͯݴ͑͹)
  27. Guice • Java༻DIϑϨʔϜϫʔΫ • ҆৺ͷGoogle੡ • ߴػೳͰग़དྷΔࣄ͕ଟ͍ • Runtime DI

  28. Guice ஫ೖ͍ͤͨ͞ͱ͜Ζʹ @Inject Λ͚ͭΔ

  29. Guice @ImplementedBy Ͱ࣮૷Λࢦఆ͢Δ or bind Ͱ഑ઢ͢Δ

  30. Guice @Named, annotatedWithͰλά෇͚΋Մೳ

  31. Guice @Named, annotatedWithͰλά෇͚΋Մೳ

  32. Guice ProviderͰॊೈͳDI͕Մೳ

  33. Guice Pros Cons • ߴػೳͰॊೈੑ͕ߴ͍ • ϘΠϥʔϓϨʔτ͕গͳ͍ • ࣮ߦ࣌ʹ഑ઢΛίϯτϩʔϧͰ͖Δ •

    JavaͰ࢖ͬͯͨਓʹ͸ೃછΈ΍͍͢ • Runtime DI • ֶशίετ͕ߴ͍
  34. ͳʹΛ࢖͑͹ྑ͍͔ʁ

  35. ԿΛ࢖͑͹ྑ͍͔ʁ ͓޷͖ͳͷΛͲ͏ͧ

  36. ͓͢͢Ί • ࠷ॳ͸Manual DI͔Β࢝ΊΔͷ͕Φεεϝ • Compile-time DIΛ࢖ͬͨ΄͏͕҆શ • Guice͸ڽͬͨࣄ͕͍ͨ͠৔߹ʹ