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ϙʔτ • RDBMS΍KVS΁ͷӬଓԽ ͷ੹຿ ηΧϯμϦϙʔτϓϩδΣΫτྫ(1) • ThirdPartyAPIϙʔτ • ThirdPartyͷAPIΛܦ༝͠ ͨίϛϡχέʔγϣϯΛ ͢Δ੹຿

Slide 47

Slide 47 text

• Persistenceϙʔτ • RDBMS΍KVSͷ۩ମతͳ࣮૷ • υϝΠϯϦϙδτϦ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

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠