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

Scalaで学ぶヘキサゴナルアーキテクチャ実践入門

24b18d2f8d7ceb0fbd521053993fba9e?s=47 kimutyam
March 26, 2016

 Scalaで学ぶヘキサゴナルアーキテクチャ実践入門

「Scala将軍達の後の祭り」での発表資料

http://scala-syogun-matsuri.connpass.com/event/28124/

24b18d2f8d7ceb0fbd521053993fba9e?s=128

kimutyam

March 26, 2016
Tweet

Transcript

  1. ScalaͰֶͿϔΩαΰφϧΞʔΩςΫ νϟ࣮ફೖ໳ ! Scalaক܉ୡͷޙͷࡇΓ @kimutyam 2016/03/26

  2. ࣗݾ঺հ ! ! ໦ଜজ޺ @kimutyam 2012/4ʹηϓςʔχೖࣾ ηϓςʔχɾΦϦδφϧͷϦʔυΤϯδχΞΛ୲౰ͯ͠·͢ 1೥લ͔Β޿ࠂͷӡ༻؅ཧπʔϧʮPYXISʯͷ։ൃΛ΍ͬͯ·͢ @j5ik2oʹࢣࣄͯ͠ɺScala/DDDΛ1೥൒͘Β͍࣮ફ͖ͯͨ͠ਓ !

    ڵຯൣғ DDD TDD ϔΩαΰφϧΞʔΩςΫνϟ ϚΠΫϩαʔϏεΞʔΩςΫνϟ
  3. ScalaͷαϯϓϧΛݩʹ࣮ફతͳϔΩαΰφϧ ΞʔΩςΫνϟͷ૊Ήํʹ͍͓ͭͯ࿩͠·͢

  4. Agenda 1. ϨΠϠʔυΞʔΩςΫνϟ͔ΒϔΩαΰφϧΞʔ ΩςΫνϟ΁ 2. ϔΩαΰφϧΞʔΩςΫνϟ֓ཁ 3. ฐࣾͰͷιϑτ΢ΣΞઓུ

  5. 1.ϨΠϠʔυΞʔΩςΫνϟ͔Βϔ ΩαΰφϧΞʔΩςΫνϟ΁

  6. Layered Architecture֓೦ਤ UI Application Domain Infrastructure Dependency

  7. Layered Architecture֓೦ਤ UI Application Domain Infrastructure Dependency • υϝΠϯ͕Πϯϑϥ ετϥΫνϟʹґଘ

    ͍ͯ͠Δ
  8. ͦ΋ͦ΋ΠϯϑϥετϥΫνϟ૚ͬ ͯʁ >্ҐͷϨΠϠΛࢧ͑ΔҰൠతͳٕज़తػೳΛఏڙ͢Δɻ ͜Εʹ͸ɺΞϓϦέʔγϣϯͷͨΊͷϝοηʔδૹ৴ɺ υϝΠϯͷͨΊͷӬଓԽɺϢʔβʔΠϯλʔϑΣʔεͷͨΊͷ΢Ο δΣοτඳըͳͲ͕͋Δɻ ΤϦοΫɾΤϰΝϯεͷυϝΠϯۦಈઃܭ http://urx.blue/sP9d ͦͷଞͷҰൠతͳٕज़తػೳͱ͸ʁ(ࢲతݟղ) •

    ݴޠ • ϢʔςΟϦςΟ • ϥΠϒϥϦ etc….
  9. υϝΠϯ૚͕ΠϯϑϥετϥΫνϟ ૚ʹґଘ͢Δ͜ͱ͸֐ѱʁ ۃ୺ͳ࿩ɺྫ͑͹ϝοηʔδૹ৴ɺӬଓԽɺ ϢʔβʔΠϯλʔϑΣʔεͷͨΊͷ΢ΟδΣοτඳը ʹ͍ͭͯཁ݅తͰมߋͷͳ͍ٕज़ج൫Ͱ͋Ε͹ґଘͯ͠΋Α͍ɻ ٕज़Λ੾Γସ͑ΔՄೳੑ͕͋ΔͷͰ͋Ε͹ɺ ۩ମతͳΠϯϑϥετϥΫνϟͷ࣮૷ʹґଘ͍ͯ͠Δ υϝΠϯͷϝϯςφϯεੑ͕௿Լ͢Δɻ ! ϢʔςΟϦςΟ΍ݴޠϨϕϧͰ·Ͱ͸ґଘΛؾʹ͢Δඞཁ͸ͳ͍ͱ

    ͑Δɻ
  10. Table Module >Table Module͸σʔλϕʔεͷςʔϒϧʹؔ࿈ͨ͠Ϗδωεϩδο ΫΛ1ͭͷΤϯςΟςΟΫϥεͱ࣮ͯ͠૷͠·͢ɻ ιϑτ΢ΣΞཁ͕݅υϝΠϯͷӬଓԽʹಛఆͷσʔλϕʔεʹ͔͠࢖ Θͳ͍৔߹͸ɺ͜ͷख๏Ͱ΋͍͍ͱߟ͑Δ υϝΠϯ૚ʹ࠷దͳΞʔΩςΫνϟΛߟ͑Δ

  11. தن໛ɾେن໛։ൃʹ͓͚Δݱ࣮ղ • ओʹI/Oͷٕज़ʹؔͯ͠͸ೖΕସ͑ͯ΋υϝΠ ϯʹӨڹ͠ͳ͍Α͏ͳઃܭͰ͋Γ͍ͨ • υϝΠϯϞσϧʹӨڹ͠ͳ͍ϥΠϒϥϦɺݴ ޠػೳ౳͸ґଘͤͯ͞΋͍͍ • ϨΠϠʔυΞʔΩςΫνϟʹ͓͍ͯͷґଘؔ ܎Ͱ໰୊ࢹ͍ͯ͠Δͷ͸υϝΠϯʹӨڹ͢Δ

    Α͏ͳٕज़͕ґଘͯ͠͠·͏ͱ͜Ζʹ͋Δɻ (ྫ͑͹ɺRepository) ※͜ΕҎ߱ͷεϥΠυ͸౎߹্Πϯϑϥετϥ Ϋνϟ͸ཁ݅มߋ༨஍ͷ͋ΔΠϯϑϥετϥΫ νϟͱͯ͠ѻ͍·͢ɻ
  12. Layered Architecture(DIP) Infrastructure UI Application Domain Dependency Domain͕ͲͷϨΠϠ ʹ΋ґଘ͍ͯ͠ͳ͍

  13. ~DIP~ґଘؔ܎ٯసͷݪଇ >ʮந৅ʯ͸࣮૷ͷৄࡉʹґଘͯ͠͸ͳΒͳ͍ɻ࣮૷ͷ ৄࡉ͕ʮந৅ʯʹґଘ͢΂͖Ͱ͋Δɻ ࣮ફυϝΠϯۦಈઃܭ (Object Oriented SELECTION) >্ҐͷϞδϡʔϧ͸ԼҐͷϞδϡʔϧʹґଘͯ͠͸ͳΒ ͳ͍ɻͲͪΒͷϞδϡʔϧ΋ʮந৅ʯʹґଘ͢΂͖Ͱ͋ Δɻ

  14. DDDͰ͸࠷্ҐͷϞδϡʔϧ͸υϝΠϯ૚ͩͱଊ͑Δͷ͕ࣗવͰɺ ্ҐϞδϡʔϧ͸υϝΠϯ૚ͷ͜ͱΛࢦ͢ɻ ࣮૷ͷํ਑͸υϝΠϯ૚ͷந৅(I/F) ʹΑܾͬͯఆ͞ΕΔɻ ~DIP~ґଘؔ܎ٯసͷݪଇ ࢲతݟղ

  15. ~DIP~ґଘؔ܎ٯసͷݪଇ ! (ݴ͍׵͑ྫ) υϝΠϯ૚͕ΠϯϑϥετϥΫνϟ૚ʹґଘͯ͠͸ͳΒ ͳ͍ɻ ͲͪΒͷϞδϡʔϧ΋υϝΠϯ૚ͷந৅ʹґଘ͢΂͖Ͱ ͋Δɻ ! >্ҐͷϞδϡʔϧ͸ԼҐͷϞδϡʔϧʹґଘͯ͠͸ͳΒ ͳ͍ɻͲͪΒͷϞδϡʔϧ΋ʮந৅ʯʹґଘ͢΂͖Ͱ͋

    Δɻ
  16. ~DIP~ґଘؔ܎ٯసͷݪଇ (ݴ͍׵͑ྫ) υϝΠϯ૚ͷந৅͸ΠϯϑϥετϥΫνϟ૚ͷ࣮૷ͷৄ ࡉʹґଘͯ͠͸ͳΒͳ͍ɻ ΠϯϑϥετϥΫνϟͷ࣮૷ͷৄࡉ͕υϝΠϯ૚ͷந৅ ʹґଘ͢΂͖Ͱ͋Δɻ >ʮந৅ʯ͸࣮૷ͷৄࡉʹґଘͯ͠͸ͳΒͳ͍ɻ࣮૷ͷ ৄࡉ͕ʮந৅ʯʹґଘ͢΂͖Ͱ͋Δɻ

  17. Layered Architecture(DIP)αϯϓϧ ίʔυ(؆қ) ! https://github.com/kimutyam/layered-dip-hands-on

  18. ύοέʔδྫ ! ϨΠϠຖʹϓϩδΣΫτ෼ׂ

  19. υϝΠϯϓϩδΣΫτ(ྫ) ! RepositoryͷI/FΛఆٛ

  20. ΠϯϑϥετϥΫνϟϓϩδΣΫτ (ྫ) ! Repositoryͷ࣮૷

  21. ٕज़͕૿͍͑ͯ͘ʹͭΕͯΠϯϑϥετϥΫνϟ૚͕ ϑΝοτʹͳΓ͕ͪ(ΠϯϑϥετϥΫνϟΛϓϩδΣΫ τ෼ׂͨ͠Βղܾ) UI͕૿͍͑ͯ͘ʹͭΕͯUI૚͕ϑΝοτʹͳΓ͕ͪ(UIΛ ϓϩδΣΫτ෼ׂͨ͠Βղܾ) Layered Architecture With DIP ΞʔΩςΫνϟϨϕϧͰΠϯϑϥετϥΫνϟͱUIͷ

    ؔ৺ͷ෼཭Λߦ͏͜ͱ͕೉͍͠
  22. 2.ϔΩαΰφϧΞʔΩςΫνϟ֓ཁ

  23. ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture຋༁)

  24. ಺ଆͱ֎ଆͷΞμϓλ ΞϓϦέʔγϣϯͷ֎ଆ ! ΞϓϦέʔγϣϯػೳཁ ݅ υϝΠϯͷϏδωε ϩδοΫ ಺ଆͱ֎ଆͷίϛϡχέʔγϣ ϯΛࣝผ͢Δϙʔτ

  25. ґଘؔ܎ਤ

  26. ॲཧͷྲྀΕ

  27. Hexagonal Architecture༻ޠ(1) • Port(ϙʔτ) ΠϯλʔϑΣʔεΛجૅʹσόΠε(αʔϏε)ؒͷձ࿩ Λࣝผ͢Δɻϙʔτͷ਺͸ઃܭऀ࣍ୈͰࣗ༝ɻ >ͳʹ͕ϙʔτͰɺͳʹ͕ͦ͏Ͱͳ͍͔͸ɺ΄ͱΜͲ޷ Έͷ໰୊ͩɻ >Θͨ͠ͷબ୒͸ɺ2,3,4ϙʔτͷখ͍͞਺ࣈΛ޷Ή܏޲ ͕͋Δɻ

    ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture຋༁)
  28. Hexagonal Architecture༻ޠ(2) • Adapter(Ξμϓλ) ΠϯλʔϑΣʔεΛ֤σόΠε(αʔϏε)͕ඞཁͱ͢Δ ৴߸ʹม͑Δ Port : Adapter =

    1 : N >σόΠε͕ඞཁͱ͢Δ৴߸ʹม͑Δɺٯ΋·ͨવΓɻ ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture຋༁)
  29. Hexagonal Architecture༻ޠ(3) • Primary Port(ϓϥΠϚϦʔϙʔτ) ΞϓϦʔέʔγϣϯΛۦಈ͢Δϙʔτͷ͜ͱΛࢦ͢

  30. Hexagonal Architecture༻ޠ(4) • Secondary Port(ηΧϯμϦʔϙʔτ) ΞϓϦʔέʔγϣϯʹۦಈ͞ΕΔϙʔτͷ͜ͱΛࢦ͢

  31. Hexagonal Architectureͷࢫຯ(1) • ϙʔτ୯ҐͰؔ৺Λ෼཭͢Δ͜ͱ͕Մೳ • υϝΠϯΛத৺ʹஔ͘ࣄͰΞϓϦέʔγϣϯશମͷํ਑ ͕ܾఆ͞ΕΔ ϙʔτؒͰͷґଘ͸ͳ͘ɺϙʔτ্Ͱ͸υϝΠϯͳ͍͠ ͸ΞϓϦέʔγϣϯͷ࣮૷Λར༻ͨ͠ΓɺI/FΛ࣮૷͢Δ •

    Ξμϓλ୯ҐͰٕज़ͷ૊Έସ͕͑Մೳ(ৄࡉͷٕज़Λมߋ ͯ͠΋υϝΠϯʹ͸Өڹ͸͠ͳ͍) • ϞοΫԽ͠΍͍͢(ϞοΫDBΛར༻͢ΔΑ͏ͳ࢓૊Έ͕ ؆୯ʹ࣮ݱͰ͖Δ) • ࢹ֮తͰॲཧͷϑϩʔ΍ґଘؔ܎͕໌ྎ
  32. Hexagonal Architectureαϯϓϧ ίʔυ https://github.com/kimutyam/hexagonal-hands-on

  33. ύοέʔδྫ ྫʹΑͬͯϨΠϠɾϙʔτ ຖʹϓϩδΣΫτ෼ׂ

  34. υϝΠϯϓϩδΣΫτྫ(1) • ίΞυϝΠϯͱ αϒυϝΠϯΛ ू໿ • ڥք͚ͮΒΕͨ ίϯςΩετؒ ͷґଘΛͳ͘͢ •

    υϝΠϯҎ֎ͷ ϓϩδΣΫτͷ ґଘ͸ͤ͞ͳ͍
  35. υϝΠϯϓϩδΣΫτྫ(2) • υϝΠϯϞσϧ (Entity,VO,Service,Event,Mo dule) • Repository,Factory,Aggrega te etc

  36. υϝΠϯϓϩδΣΫτྫ(2)

  37. ΞϓϦέʔγϣϯϓϩδΣΫτྫ(1) ΞϓϦέʔγϣϯϓϩδΣΫτ ֤υϝΠϯΛґଘͤ͞Δ

  38. ΞϓϦέʔγϣϯϓϩδΣΫτྫ(2) • ΞϓϦέʔγϣϯαʔϏε ࠞཚ͕ͪ͠ͳαʔϏεͱ͍͏֓೦ʹ͍ͭͯ υϝΠϯαʔϏεͱΞϓϦέʔγϣϯαʔϏεͷҧ͍ ʹ͍ͭͯ͸ҎԼࢀর

  39. ΞϓϦέʔγϣϯϓϩδΣΫτྫ(3) ڥք͚ͮΒΕͨυϝΠϯίϯ ςΩετͷConverter(Mapper)

  40. ΞϓϦέʔγϣϯϓϩδΣΫτྫ(4) DTO(Data Transfer Object)

  41. ϙʔτϓϩδΣΫτྫ(1) • ϙʔτϓϩδΣΫ τ ϓϥΠϚϦʔϙʔ τͱηΧϯμϦϙʔ τΛू໿ • ϓϥΠϚϦʔϙʔ τ

    • ηΧϯμϦʔϙʔ τ ! ※ϙʔτؒͷґଘ͸ ͳ͠
  42. ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (1) • WebServiceϙʔτ • HttpʹΑͬͯۦಈ͞ΕΔ • PlayFrameworkΛར༻

  43. ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (2) • WebService ϙʔτ • Controll er • Router

    • json • Form
  44. ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (3) • Batchϙʔτ • CLIͰ࣮ߦ͢Δ

  45. ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (4) • Batchϙʔτ • Batch • Scheduler etc…

  46. • Persistenceϙʔτ • RDBMS΍KVS΁ͷӬଓԽ ͷ੹຿ ηΧϯμϦϙʔτϓϩδΣΫτྫ(1) • ThirdPartyAPIϙʔτ • ThirdPartyͷAPIΛܦ༝͠

    ͨίϛϡχέʔγϣϯΛ ͢Δ੹຿
  47. • Persistenceϙʔτ • RDBMS΍KVSͷ۩ମతͳ࣮૷ • υϝΠϯϦϙδτϦI/Fͷํ਑ʹै͏ • DAO ηΧϯμϦϙʔτϓϩδΣΫτྫ(2)

  48. • ThirdPartyAPIϙʔτ • APIClient • υϝΠϯϦϙδτϦI/Fʹ ै࣮ͬͨ૷ ηΧϯμϦϙʔτϓϩδΣΫτྫ(3)

  49. • ֤ϓϩδΣΫτΛू໿ɾґଘ • ·ͱΊ໿ RootϓϩδΣΫτྫ(1)

  50. • DependencyInjection • ΞϓϦέʔγϣϯͷઃఆϑΝ Πϧ౳ RootϓϩδΣΫτྫ(2)

  51. Hexagonal Architectureͷࢫຯ(2) ϔΩαΰφϧΞʔΩςΫνϟ͸ෳ਺ͷ֎෦αʔϏεͱͷ࿈ ܞ͕࣮૷͠΍͍͢ ํ਑͸த৺ʹυϝΠϯΛਾ͑Δɹˠɹ౷ҰతͳઃܭΛҡ ࣋͢Δ͜ͱ͕Մೳ

  52. 3.ฐࣾͰͷιϑτ΢ΣΞઓུ

  53. ฐࣾͰߦ͍ͬͯΔઓུ ઓུతυϝΠϯ ઃܭ ઓज़తυϝΠϯ ઃܭ ৄࡉ࣮૷ ϞοΫ࣮૷ ϞϊϦγοΫΞϓϦέʔγϣϯ

  54. ฐࣾͰߦ͍ͬͯΔઓུ ઓུతυϝΠϯ ઃܭ ઓज़తυϝΠϯ ઃܭ ৄࡉ࣮૷ ϞοΫ࣮૷ ઓུతυϝΠϯ ઃܭ ઓज़తυϝΠϯ

    ઃܭ ৄࡉ࣮૷ ϞοΫ࣮૷ ڥք͚ͮΒΕͨίϯςΩετ୯ҐͰϚ ΠΫϩαʔϏεԽ
  55. ϚΠΫϩαʔϏεΞʔΩςΫνϟͷΠϝʔδ !

  56. ࣮ફ౤ೖͯ͠ΈͯͷϝϦοτ·ͱΊ • ϓϩδΣΫτɾϓϩμΫτಋೖظͷυϝΠϯΛৠཹͤ͞ ͳ͕ΒɺϞοΩϯά͕͠΍͍͢ • ґଘؔ܎ͷ੍໿͕ڧ͍ΞʔΩςΫνϟͳͷͰίʔυͷํ ਑͕ͿΕͮΒ͍ • ৽͍ٕ͠ज़Λಋೖ͠΍͍͢ •

    ϚΠΫϩαʔϏεͱͷ૬ੑ͕Α͍ • ϨΠϠʔυΞʔΩςΫνϟDIPͱൺ΂ͯؔ৺ͷ෼཭͕͠΍ ͘͢ • ΞʔΩςΫνϟਤͷࢹ֮ޮՌʹΑΓϝϯόʔͷཧղ͕͸ ΍͍ ! !
  57. ࣮ફ౤ೖͯ͠Έͯ೰Μͩ͜ͱ·ͱΊ • Batchͷ։ൃΛBatchϙʔτͰߦ͍͕ͬͯͨɺ ඞͣ͠΋υϝΠϯΛܦ༝͢ΔBatchͰ͸ͳ͔ͬͨɻ
 (MySQLͷύʔςΟγϣϯ௥Ճͱ͔) →ɹRootϓϩδΣΫτͷઃఆͱͯ͠ఆ͍ٛͯ͠Δ →ɹͦΕΑ͏ͷϓϩδΣΫτΛ੾Δͷ΋͋Γͩͱߟ͑Δ ! • ϙʔτؒͷίϛϡχέʔγϣϯ͕Ͱ͖ͳ͍ͷͰɺ

    ͪΐͬͱ࣮ͨ͠૷Λॻ͘ͱ͖Ͱ΋υϝΠϯܦ༝Ͱ࣮ݱΛߟ ͑Δඞཁ͕͋ͬͨ →ɹ͍͍ҙຯͰ΋ѱ͍ҙຯͰ΋੍໿͕͋ΓɺΞ΢τϓοτ ͕஗͘ͳΔέʔε͕͋Γ·͢ɻ׳ΕΕ͹ͳΜͯ͜ͱͳ͍Ͱ ͕͢ɻ
  58. ࢀߟࢿྉ ॻ੶ ʰ࣮ફυϝΠϯۦಈઃܭ (Object Oriented SELECTION)ʱ ॻ੶ʰΤϦοΫɾΤϰΝϯεͷυϝΠϯۦಈઃܭʱ ϔΩαΰφϧΞʔΩςΫνϟͱϨΠϠʔΞʔΩςΫνϟͷҧ͍ͱϚΠΫϩαʔϏε - falsandtru

    ͷϝϞா υϝΠϯ૚ʹ࠷దͳΞʔΩςΫνϟΛߟ͑Δ ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture຋༁) ΦϒδΣΫτࢥߟ: ґଘؔ܎ٯసͷݪଇ ࠞཚ͕ͪ͠ͳαʔϏεͱ͍͏֓೦ʹ͍ͭͯ
  59. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠