Scalaで学ぶヘキサゴナルアーキテクチャ実践入門
by
kimutyam
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
ScalaͰֶͿϔΩαΰφϧΞʔΩςΫ νϟ࣮ફೖ ! Scalaক܉ୡͷޙͷࡇΓ @kimutyam 2016/03/26
Slide 2
Slide 2 text
ࣗݾհ ! ! ଜজ @kimutyam 2012/4ʹηϓςʔχೖࣾ ηϓςʔχɾΦϦδφϧͷϦʔυΤϯδχΞΛ୲ͯ͠·͢ 1લ͔Βࠂͷӡ༻ཧπʔϧʮPYXISʯͷ։ൃΛͬͯ·͢ @j5ik2oʹࢣࣄͯ͠ɺScala/DDDΛ1͘Β͍࣮ફ͖ͯͨ͠ਓ ! ڵຯൣғ DDD TDD ϔΩαΰφϧΞʔΩςΫνϟ ϚΠΫϩαʔϏεΞʔΩςΫνϟ
Slide 3
Slide 3 text
ScalaͷαϯϓϧΛݩʹ࣮ફతͳϔΩαΰφϧ ΞʔΩςΫνϟͷΉํʹ͍͓ͭͯ͠·͢
Slide 4
Slide 4 text
Agenda 1. ϨΠϠʔυΞʔΩςΫνϟ͔ΒϔΩαΰφϧΞʔ ΩςΫνϟ 2. ϔΩαΰφϧΞʔΩςΫνϟ֓ཁ 3. ฐࣾͰͷιϑτΣΞઓུ
Slide 5
Slide 5 text
1.ϨΠϠʔυΞʔΩςΫνϟ͔Βϔ ΩαΰφϧΞʔΩςΫνϟ
Slide 6
Slide 6 text
Layered Architecture֓೦ਤ UI Application Domain Infrastructure Dependency
Slide 7
Slide 7 text
Layered Architecture֓೦ਤ UI Application Domain Infrastructure Dependency • υϝΠϯ͕Πϯϑϥ ετϥΫνϟʹґଘ ͍ͯ͠Δ
Slide 8
Slide 8 text
ͦͦΠϯϑϥετϥΫνϟͬ ͯʁ >্ҐͷϨΠϠΛࢧ͑ΔҰൠతͳٕज़తػೳΛఏڙ͢Δɻ ͜ΕʹɺΞϓϦέʔγϣϯͷͨΊͷϝοηʔδૹ৴ɺ υϝΠϯͷͨΊͷӬଓԽɺϢʔβʔΠϯλʔϑΣʔεͷͨΊͷΟ δΣοτඳըͳͲ͕͋Δɻ ΤϦοΫɾΤϰΝϯεͷυϝΠϯۦಈઃܭ http://urx.blue/sP9d ͦͷଞͷҰൠతͳٕज़తػೳͱʁ(ࢲతݟղ) • ݴޠ • ϢʔςΟϦςΟ • ϥΠϒϥϦ etc….
Slide 9
Slide 9 text
υϝΠϯ͕ΠϯϑϥετϥΫνϟ ʹґଘ͢Δ͜ͱѱʁ ۃͳɺྫ͑ϝοηʔδૹ৴ɺӬଓԽɺ ϢʔβʔΠϯλʔϑΣʔεͷͨΊͷΟδΣοτඳը ʹ͍ͭͯཁ݅తͰมߋͷͳ͍ٕज़ج൫Ͱ͋Εґଘͯ͠Α͍ɻ ٕज़ΛΓସ͑ΔՄೳੑ͕͋ΔͷͰ͋Εɺ ۩ମతͳΠϯϑϥετϥΫνϟͷ࣮ʹґଘ͍ͯ͠Δ υϝΠϯͷϝϯςφϯεੑ͕Լ͢Δɻ ! ϢʔςΟϦςΟݴޠϨϕϧͰ·ͰґଘΛؾʹ͢Δඞཁͳ͍ͱ ͑Δɻ
Slide 10
Slide 10 text
Table Module >Table Moduleσʔλϕʔεͷςʔϒϧʹؔ࿈ͨ͠Ϗδωεϩδο ΫΛ1ͭͷΤϯςΟςΟΫϥεͱ࣮ͯ͠͠·͢ɻ ιϑτΣΞཁ͕݅υϝΠϯͷӬଓԽʹಛఆͷσʔλϕʔεʹ͔͠ Θͳ͍߹ɺ͜ͷख๏Ͱ͍͍ͱߟ͑Δ υϝΠϯʹ࠷దͳΞʔΩςΫνϟΛߟ͑Δ
Slide 11
Slide 11 text
தنɾେن։ൃʹ͓͚Δݱ࣮ղ • ओʹI/Oͷٕज़ʹؔͯ͠ೖΕସ͑ͯυϝΠ ϯʹӨڹ͠ͳ͍Α͏ͳઃܭͰ͋Γ͍ͨ • υϝΠϯϞσϧʹӨڹ͠ͳ͍ϥΠϒϥϦɺݴ ޠػೳґଘ͍͍ͤͯ͞ • ϨΠϠʔυΞʔΩςΫνϟʹ͓͍ͯͷґଘؔ Ͱࢹ͍ͯ͠ΔͷυϝΠϯʹӨڹ͢Δ Α͏ͳٕज़͕ґଘͯ͠͠·͏ͱ͜Ζʹ͋Δɻ (ྫ͑ɺRepository) ※͜ΕҎ߱ͷεϥΠυ߹্Πϯϑϥετϥ Ϋνϟཁ݅มߋ༨ͷ͋ΔΠϯϑϥετϥΫ νϟͱͯ͠ѻ͍·͢ɻ
Slide 12
Slide 12 text
Layered Architecture(DIP) Infrastructure UI Application Domain Dependency Domain͕ͲͷϨΠϠ ʹґଘ͍ͯ͠ͳ͍
Slide 13
Slide 13 text
~DIP~ґଘؔٯసͷݪଇ >ʮநʯ࣮ͷৄࡉʹґଘͯ͠ͳΒͳ͍ɻ࣮ͷ ৄࡉ͕ʮநʯʹґଘ͖͢Ͱ͋Δɻ ࣮ફυϝΠϯۦಈઃܭ (Object Oriented SELECTION) >্ҐͷϞδϡʔϧԼҐͷϞδϡʔϧʹґଘͯ͠ͳΒ ͳ͍ɻͲͪΒͷϞδϡʔϧʮநʯʹґଘ͖͢Ͱ͋ Δɻ
Slide 14
Slide 14 text
DDDͰ࠷্ҐͷϞδϡʔϧυϝΠϯͩͱଊ͑Δͷ͕ࣗવͰɺ ্ҐϞδϡʔϧυϝΠϯͷ͜ͱΛࢦ͢ɻ ࣮ͷํυϝΠϯͷந(I/F) ʹΑܾͬͯఆ͞ΕΔɻ ~DIP~ґଘؔٯసͷݪଇ ࢲతݟղ
Slide 15
Slide 15 text
~DIP~ґଘؔٯసͷݪଇ ! (ݴ͍͑ྫ) υϝΠϯ͕ΠϯϑϥετϥΫνϟʹґଘͯ͠ͳΒ ͳ͍ɻ ͲͪΒͷϞδϡʔϧυϝΠϯͷநʹґଘ͖͢Ͱ ͋Δɻ ! >্ҐͷϞδϡʔϧԼҐͷϞδϡʔϧʹґଘͯ͠ͳΒ ͳ͍ɻͲͪΒͷϞδϡʔϧʮநʯʹґଘ͖͢Ͱ͋ Δɻ
Slide 16
Slide 16 text
~DIP~ґଘؔٯసͷݪଇ (ݴ͍͑ྫ) υϝΠϯͷநΠϯϑϥετϥΫνϟͷ࣮ͷৄ ࡉʹґଘͯ͠ͳΒͳ͍ɻ ΠϯϑϥετϥΫνϟͷ࣮ͷৄࡉ͕υϝΠϯͷந ʹґଘ͖͢Ͱ͋Δɻ >ʮநʯ࣮ͷৄࡉʹґଘͯ͠ͳΒͳ͍ɻ࣮ͷ ৄࡉ͕ʮநʯʹґଘ͖͢Ͱ͋Δɻ
Slide 17
Slide 17 text
Layered Architecture(DIP)αϯϓϧ ίʔυ(؆қ) ! https://github.com/kimutyam/layered-dip-hands-on
Slide 18
Slide 18 text
ύοέʔδྫ ! ϨΠϠຖʹϓϩδΣΫτׂ
Slide 19
Slide 19 text
υϝΠϯϓϩδΣΫτ(ྫ) ! RepositoryͷI/FΛఆٛ
Slide 20
Slide 20 text
ΠϯϑϥετϥΫνϟϓϩδΣΫτ (ྫ) ! Repositoryͷ࣮
Slide 21
Slide 21 text
ٕज़͕૿͍͑ͯ͘ʹͭΕͯΠϯϑϥετϥΫνϟ͕ ϑΝοτʹͳΓ͕ͪ(ΠϯϑϥετϥΫνϟΛϓϩδΣΫ τׂͨ͠Βղܾ) UI͕૿͍͑ͯ͘ʹͭΕͯUI͕ϑΝοτʹͳΓ͕ͪ(UIΛ ϓϩδΣΫτׂͨ͠Βղܾ) Layered Architecture With DIP ΞʔΩςΫνϟϨϕϧͰΠϯϑϥετϥΫνϟͱUIͷ ؔ৺ͷΛߦ͏͜ͱ͕͍͠
Slide 22
Slide 22 text
2.ϔΩαΰφϧΞʔΩςΫνϟ֓ཁ
Slide 23
Slide 23 text
ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture༁)
Slide 24
Slide 24 text
ଆͱ֎ଆͷΞμϓλ ΞϓϦέʔγϣϯͷ֎ଆ ! ΞϓϦέʔγϣϯػೳཁ ݅ υϝΠϯͷϏδωε ϩδοΫ ଆͱ֎ଆͷίϛϡχέʔγϣ ϯΛࣝผ͢Δϙʔτ
Slide 25
Slide 25 text
ґଘؔਤ
Slide 26
Slide 26 text
ॲཧͷྲྀΕ
Slide 27
Slide 27 text
Hexagonal Architecture༻ޠ(1) • Port(ϙʔτ) ΠϯλʔϑΣʔεΛجૅʹσόΠε(αʔϏε)ؒͷձ Λࣝผ͢Δɻϙʔτͷઃܭऀ࣍ୈͰࣗ༝ɻ >ͳʹ͕ϙʔτͰɺͳʹ͕ͦ͏Ͱͳ͍͔ɺ΄ͱΜͲ Έͷͩɻ >Θͨ͠ͷબɺ2,3,4ϙʔτͷখ͍͞ࣈΛΉ ͕͋Δɻ ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture༁)
Slide 28
Slide 28 text
Hexagonal Architecture༻ޠ(2) • Adapter(Ξμϓλ) ΠϯλʔϑΣʔεΛ֤σόΠε(αʔϏε)͕ඞཁͱ͢Δ ৴߸ʹม͑Δ Port : Adapter = 1 : N >σόΠε͕ඞཁͱ͢Δ৴߸ʹม͑Δɺٯ·ͨવΓɻ ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture༁)
Slide 29
Slide 29 text
Hexagonal Architecture༻ޠ(3) • Primary Port(ϓϥΠϚϦʔϙʔτ) ΞϓϦʔέʔγϣϯΛۦಈ͢Δϙʔτͷ͜ͱΛࢦ͢
Slide 30
Slide 30 text
Hexagonal Architecture༻ޠ(4) • Secondary Port(ηΧϯμϦʔϙʔτ) ΞϓϦʔέʔγϣϯʹۦಈ͞ΕΔϙʔτͷ͜ͱΛࢦ͢
Slide 31
Slide 31 text
Hexagonal Architectureͷࢫຯ(1) • ϙʔτ୯ҐͰؔ৺Λ͢Δ͜ͱ͕Մೳ • υϝΠϯΛத৺ʹஔ͘ࣄͰΞϓϦέʔγϣϯશମͷํ ͕ܾఆ͞ΕΔ ϙʔτؒͰͷґଘͳ͘ɺϙʔτ্ͰυϝΠϯͳ͍͠ ΞϓϦέʔγϣϯͷ࣮Λར༻ͨ͠ΓɺI/FΛ࣮͢Δ • Ξμϓλ୯ҐͰٕज़ͷΈସ͕͑Մೳ(ৄࡉͷٕज़Λมߋ ͯ͠υϝΠϯʹӨڹ͠ͳ͍) • ϞοΫԽ͍͢͠(ϞοΫDBΛར༻͢ΔΑ͏ͳΈ͕ ؆୯ʹ࣮ݱͰ͖Δ) • ࢹ֮తͰॲཧͷϑϩʔґଘ͕ؔ໌ྎ
Slide 32
Slide 32 text
Hexagonal Architectureαϯϓϧ ίʔυ https://github.com/kimutyam/hexagonal-hands-on
Slide 33
Slide 33 text
ύοέʔδྫ ྫʹΑͬͯϨΠϠɾϙʔτ ຖʹϓϩδΣΫτׂ
Slide 34
Slide 34 text
υϝΠϯϓϩδΣΫτྫ(1) • ίΞυϝΠϯͱ αϒυϝΠϯΛ ू • ڥք͚ͮΒΕͨ ίϯςΩετؒ ͷґଘΛͳ͘͢ • υϝΠϯҎ֎ͷ ϓϩδΣΫτͷ ґଘͤ͞ͳ͍
Slide 35
Slide 35 text
υϝΠϯϓϩδΣΫτྫ(2) • υϝΠϯϞσϧ (Entity,VO,Service,Event,Mo dule) • Repository,Factory,Aggrega te etc
Slide 36
Slide 36 text
υϝΠϯϓϩδΣΫτྫ(2)
Slide 37
Slide 37 text
ΞϓϦέʔγϣϯϓϩδΣΫτྫ(1) ΞϓϦέʔγϣϯϓϩδΣΫτ ֤υϝΠϯΛґଘͤ͞Δ
Slide 38
Slide 38 text
ΞϓϦέʔγϣϯϓϩδΣΫτྫ(2) • ΞϓϦέʔγϣϯαʔϏε ࠞཚ͕ͪ͠ͳαʔϏεͱ͍͏֓೦ʹ͍ͭͯ υϝΠϯαʔϏεͱΞϓϦέʔγϣϯαʔϏεͷҧ͍ ʹ͍ͭͯҎԼࢀর
Slide 39
Slide 39 text
ΞϓϦέʔγϣϯϓϩδΣΫτྫ(3) ڥք͚ͮΒΕͨυϝΠϯίϯ ςΩετͷConverter(Mapper)
Slide 40
Slide 40 text
ΞϓϦέʔγϣϯϓϩδΣΫτྫ(4) DTO(Data Transfer Object)
Slide 41
Slide 41 text
ϙʔτϓϩδΣΫτྫ(1) • ϙʔτϓϩδΣΫ τ ϓϥΠϚϦʔϙʔ τͱηΧϯμϦϙʔ τΛू • ϓϥΠϚϦʔϙʔ τ • ηΧϯμϦʔϙʔ τ ! ※ϙʔτؒͷґଘ ͳ͠
Slide 42
Slide 42 text
ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (1) • WebServiceϙʔτ • HttpʹΑͬͯۦಈ͞ΕΔ • PlayFrameworkΛར༻
Slide 43
Slide 43 text
ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (2) • WebService ϙʔτ • Controll er • Router • json • Form
Slide 44
Slide 44 text
ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (3) • Batchϙʔτ • CLIͰ࣮ߦ͢Δ
Slide 45
Slide 45 text
ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (4) • Batchϙʔτ • Batch • Scheduler etc…
Slide 46
Slide 46 text
• Persistenceϙʔτ • RDBMSKVSͷӬଓԽ ͷ ηΧϯμϦϙʔτϓϩδΣΫτྫ(1) • ThirdPartyAPIϙʔτ • ThirdPartyͷAPIΛܦ༝͠ ͨίϛϡχέʔγϣϯΛ ͢Δ
Slide 47
Slide 47 text
• Persistenceϙʔτ • RDBMSKVSͷ۩ମతͳ࣮ • υϝΠϯϦϙδτϦI/Fͷํʹै͏ • DAO ηΧϯμϦϙʔτϓϩδΣΫτྫ(2)
Slide 48
Slide 48 text
• ThirdPartyAPIϙʔτ • APIClient • υϝΠϯϦϙδτϦI/Fʹ ै࣮ͬͨ ηΧϯμϦϙʔτϓϩδΣΫτྫ(3)
Slide 49
Slide 49 text
• ֤ϓϩδΣΫτΛूɾґଘ • ·ͱΊ RootϓϩδΣΫτྫ(1)
Slide 50
Slide 50 text
• DependencyInjection • ΞϓϦέʔγϣϯͷઃఆϑΝ Πϧ RootϓϩδΣΫτྫ(2)
Slide 51
Slide 51 text
Hexagonal Architectureͷࢫຯ(2) ϔΩαΰφϧΞʔΩςΫνϟෳͷ֎෦αʔϏεͱͷ࿈ ܞ͕࣮͍͢͠ ํத৺ʹυϝΠϯΛਾ͑Δɹˠɹ౷ҰతͳઃܭΛҡ ࣋͢Δ͜ͱ͕Մೳ
Slide 52
Slide 52 text
3.ฐࣾͰͷιϑτΣΞઓུ
Slide 53
Slide 53 text
ฐࣾͰߦ͍ͬͯΔઓུ ઓུతυϝΠϯ ઃܭ ઓज़తυϝΠϯ ઃܭ ৄࡉ࣮ ϞοΫ࣮ ϞϊϦγοΫΞϓϦέʔγϣϯ
Slide 54
Slide 54 text
ฐࣾͰߦ͍ͬͯΔઓུ ઓུతυϝΠϯ ઃܭ ઓज़తυϝΠϯ ઃܭ ৄࡉ࣮ ϞοΫ࣮ ઓུతυϝΠϯ ઃܭ ઓज़తυϝΠϯ ઃܭ ৄࡉ࣮ ϞοΫ࣮ ڥք͚ͮΒΕͨίϯςΩετ୯ҐͰϚ ΠΫϩαʔϏεԽ
Slide 55
Slide 55 text
ϚΠΫϩαʔϏεΞʔΩςΫνϟͷΠϝʔδ !
Slide 56
Slide 56 text
࣮ફೖͯ͠ΈͯͷϝϦοτ·ͱΊ • ϓϩδΣΫτɾϓϩμΫτಋೖظͷυϝΠϯΛৠཹͤ͞ ͳ͕ΒɺϞοΩϯά͕͍͢͠ • ґଘؔͷ੍͕ڧ͍ΞʔΩςΫνϟͳͷͰίʔυͷํ ͕ͿΕͮΒ͍ • ৽͍ٕ͠ज़Λಋೖ͍͢͠ • ϚΠΫϩαʔϏεͱͷ૬ੑ͕Α͍ • ϨΠϠʔυΞʔΩςΫνϟDIPͱൺͯؔ৺ͷ͕͠ ͘͢ • ΞʔΩςΫνϟਤͷࢹ֮ޮՌʹΑΓϝϯόʔͷཧղ͕ ͍ ! !
Slide 57
Slide 57 text
࣮ફೖͯ͠ΈͯΜͩ͜ͱ·ͱΊ • Batchͷ։ൃΛBatchϙʔτͰߦ͍͕ͬͯͨɺ ඞͣ͠υϝΠϯΛܦ༝͢ΔBatchͰͳ͔ͬͨɻ (MySQLͷύʔςΟγϣϯՃͱ͔) →ɹRootϓϩδΣΫτͷઃఆͱͯ͠ఆ͍ٛͯ͠Δ →ɹͦΕΑ͏ͷϓϩδΣΫτΛΔͷ͋Γͩͱߟ͑Δ ! • ϙʔτؒͷίϛϡχέʔγϣϯ͕Ͱ͖ͳ͍ͷͰɺ ͪΐͬͱ࣮ͨ͠Λॻ͘ͱ͖ͰυϝΠϯܦ༝Ͱ࣮ݱΛߟ ͑Δඞཁ͕͋ͬͨ →ɹ͍͍ҙຯͰѱ͍ҙຯͰ੍͕͋ΓɺΞτϓοτ ͕͘ͳΔέʔε͕͋Γ·͢ɻ׳ΕΕͳΜͯ͜ͱͳ͍Ͱ ͕͢ɻ
Slide 58
Slide 58 text
ࢀߟࢿྉ ॻ੶ ʰ࣮ફυϝΠϯۦಈઃܭ (Object Oriented SELECTION)ʱ ॻ੶ʰΤϦοΫɾΤϰΝϯεͷυϝΠϯۦಈઃܭʱ ϔΩαΰφϧΞʔΩςΫνϟͱϨΠϠʔΞʔΩςΫνϟͷҧ͍ͱϚΠΫϩαʔϏε - falsandtru ͷϝϞா υϝΠϯʹ࠷దͳΞʔΩςΫνϟΛߟ͑Δ ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture༁) ΦϒδΣΫτࢥߟ: ґଘؔٯసͷݪଇ ࠞཚ͕ͪ͠ͳαʔϏεͱ͍͏֓೦ʹ͍ͭͯ
Slide 59
Slide 59 text
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠