Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Scalaで学ぶヘキサゴナルアーキテクチャ実践入門
Search
kimutyam
March 26, 2016
Technology
15
6.6k
Scalaで学ぶヘキサゴナルアーキテクチャ実践入門
「Scala将軍達の後の祭り」での発表資料
http://scala-syogun-matsuri.connpass.com/event/28124/
kimutyam
March 26, 2016
Tweet
Share
More Decks by kimutyam
See All by kimutyam
NestJSのDIコンテナで作るクリーンなレイヤー境界
kimutyam
2
1.8k
Embulk / Presto / Sparkを用いたETL事情
kimutyam
4
2.1k
セプテーニで分析基盤(Treasure Data)を導入した話
kimutyam
0
1.4k
Reactive Messaging Patternsを使った境界づけられたコンテキストの統合
kimutyam
3
1.2k
アジャイルでのドメイン・ユースケースモデリング
kimutyam
5
2.2k
Introduction of ScalaTest
kimutyam
3
2.1k
Other Decks in Technology
See All in Technology
Lexical Analysis
shigashiyama
1
150
TanStack Routerに移行するのかい しないのかい、どっちなんだい! / Are you going to migrate to TanStack Router or not? Which one is it?
kaminashi
0
600
TypeScriptの次なる大進化なるか!? 条件型を返り値とする関数の型推論
uhyo
2
1.7k
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
390
Taming you application's environments
salaboy
0
190
適材適所の技術選定 〜GraphQL・REST API・tRPC〜 / Optimal Technology Selection
kakehashi
1
690
AI前提のサービス運用ってなんだろう?
ryuichi1208
8
1.4k
DynamoDB でスロットリングが発生したとき_大盛りver/when_throttling_occurs_in_dynamodb_long
emiki
1
430
OTelCol_TailSampling_and_SpanMetrics
gumamon
1
190
Evangelismo técnico: ¿qué, cómo y por qué?
trishagee
0
360
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
180
誰も全体を知らない ~ ロールの垣根を超えて引き上げる開発生産性 / Boosting Development Productivity Across Roles
kakehashi
1
230
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Designing the Hi-DPI Web
ddemaree
280
34k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Automating Front-end Workflow
addyosmani
1366
200k
Designing Experiences People Love
moore
138
23k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Faster Mobile Websites
deanohume
305
30k
Transcript
ScalaͰֶͿϔΩαΰφϧΞʔΩςΫ νϟ࣮ફೖ ! Scalaক܉ୡͷޙͷࡇΓ @kimutyam 2016/03/26
ࣗݾհ ! ! ଜজ @kimutyam 2012/4ʹηϓςʔχೖࣾ ηϓςʔχɾΦϦδφϧͷϦʔυΤϯδχΞΛ୲ͯ͠·͢ 1લ͔Βࠂͷӡ༻ཧπʔϧʮPYXISʯͷ։ൃΛͬͯ·͢ @j5ik2oʹࢣࣄͯ͠ɺScala/DDDΛ1͘Β͍࣮ફ͖ͯͨ͠ਓ !
ڵຯൣғ DDD TDD ϔΩαΰφϧΞʔΩςΫνϟ ϚΠΫϩαʔϏεΞʔΩςΫνϟ
ScalaͷαϯϓϧΛݩʹ࣮ફతͳϔΩαΰφϧ ΞʔΩςΫνϟͷΉํʹ͍͓ͭͯ͠·͢
Agenda 1. ϨΠϠʔυΞʔΩςΫνϟ͔ΒϔΩαΰφϧΞʔ ΩςΫνϟ 2. ϔΩαΰφϧΞʔΩςΫνϟ֓ཁ 3. ฐࣾͰͷιϑτΣΞઓུ
1.ϨΠϠʔυΞʔΩςΫνϟ͔Βϔ ΩαΰφϧΞʔΩςΫνϟ
Layered Architecture֓೦ਤ UI Application Domain Infrastructure Dependency
Layered Architecture֓೦ਤ UI Application Domain Infrastructure Dependency • υϝΠϯ͕Πϯϑϥ ετϥΫνϟʹґଘ
͍ͯ͠Δ
ͦͦΠϯϑϥετϥΫνϟͬ ͯʁ >্ҐͷϨΠϠΛࢧ͑ΔҰൠతͳٕज़తػೳΛఏڙ͢Δɻ ͜ΕʹɺΞϓϦέʔγϣϯͷͨΊͷϝοηʔδૹ৴ɺ υϝΠϯͷͨΊͷӬଓԽɺϢʔβʔΠϯλʔϑΣʔεͷͨΊͷΟ δΣοτඳըͳͲ͕͋Δɻ ΤϦοΫɾΤϰΝϯεͷυϝΠϯۦಈઃܭ http://urx.blue/sP9d ͦͷଞͷҰൠతͳٕज़తػೳͱʁ(ࢲతݟղ) •
ݴޠ • ϢʔςΟϦςΟ • ϥΠϒϥϦ etc….
υϝΠϯ͕ΠϯϑϥετϥΫνϟ ʹґଘ͢Δ͜ͱѱʁ ۃͳɺྫ͑ϝοηʔδૹ৴ɺӬଓԽɺ ϢʔβʔΠϯλʔϑΣʔεͷͨΊͷΟδΣοτඳը ʹ͍ͭͯཁ݅తͰมߋͷͳ͍ٕज़ج൫Ͱ͋Εґଘͯ͠Α͍ɻ ٕज़ΛΓସ͑ΔՄೳੑ͕͋ΔͷͰ͋Εɺ ۩ମతͳΠϯϑϥετϥΫνϟͷ࣮ʹґଘ͍ͯ͠Δ υϝΠϯͷϝϯςφϯεੑ͕Լ͢Δɻ ! ϢʔςΟϦςΟݴޠϨϕϧͰ·ͰґଘΛؾʹ͢Δඞཁͳ͍ͱ
͑Δɻ
Table Module >Table Moduleσʔλϕʔεͷςʔϒϧʹؔ࿈ͨ͠Ϗδωεϩδο ΫΛ1ͭͷΤϯςΟςΟΫϥεͱ࣮ͯ͠͠·͢ɻ ιϑτΣΞཁ͕݅υϝΠϯͷӬଓԽʹಛఆͷσʔλϕʔεʹ͔͠ Θͳ͍߹ɺ͜ͷख๏Ͱ͍͍ͱߟ͑Δ υϝΠϯʹ࠷దͳΞʔΩςΫνϟΛߟ͑Δ
தنɾେن։ൃʹ͓͚Δݱ࣮ղ • ओʹI/Oͷٕज़ʹؔͯ͠ೖΕସ͑ͯυϝΠ ϯʹӨڹ͠ͳ͍Α͏ͳઃܭͰ͋Γ͍ͨ • υϝΠϯϞσϧʹӨڹ͠ͳ͍ϥΠϒϥϦɺݴ ޠػೳґଘ͍͍ͤͯ͞ • ϨΠϠʔυΞʔΩςΫνϟʹ͓͍ͯͷґଘؔ Ͱࢹ͍ͯ͠ΔͷυϝΠϯʹӨڹ͢Δ
Α͏ͳٕज़͕ґଘͯ͠͠·͏ͱ͜Ζʹ͋Δɻ (ྫ͑ɺRepository) ※͜ΕҎ߱ͷεϥΠυ߹্Πϯϑϥετϥ Ϋνϟཁ݅มߋ༨ͷ͋ΔΠϯϑϥετϥΫ νϟͱͯ͠ѻ͍·͢ɻ
Layered Architecture(DIP) Infrastructure UI Application Domain Dependency Domain͕ͲͷϨΠϠ ʹґଘ͍ͯ͠ͳ͍
~DIP~ґଘؔٯసͷݪଇ >ʮநʯ࣮ͷৄࡉʹґଘͯ͠ͳΒͳ͍ɻ࣮ͷ ৄࡉ͕ʮநʯʹґଘ͖͢Ͱ͋Δɻ ࣮ફυϝΠϯۦಈઃܭ (Object Oriented SELECTION) >্ҐͷϞδϡʔϧԼҐͷϞδϡʔϧʹґଘͯ͠ͳΒ ͳ͍ɻͲͪΒͷϞδϡʔϧʮநʯʹґଘ͖͢Ͱ͋ Δɻ
DDDͰ࠷্ҐͷϞδϡʔϧυϝΠϯͩͱଊ͑Δͷ͕ࣗવͰɺ ্ҐϞδϡʔϧυϝΠϯͷ͜ͱΛࢦ͢ɻ ࣮ͷํυϝΠϯͷந(I/F) ʹΑܾͬͯఆ͞ΕΔɻ ~DIP~ґଘؔٯసͷݪଇ ࢲతݟղ
~DIP~ґଘؔٯసͷݪଇ ! (ݴ͍͑ྫ) υϝΠϯ͕ΠϯϑϥετϥΫνϟʹґଘͯ͠ͳΒ ͳ͍ɻ ͲͪΒͷϞδϡʔϧυϝΠϯͷநʹґଘ͖͢Ͱ ͋Δɻ ! >্ҐͷϞδϡʔϧԼҐͷϞδϡʔϧʹґଘͯ͠ͳΒ ͳ͍ɻͲͪΒͷϞδϡʔϧʮநʯʹґଘ͖͢Ͱ͋
Δɻ
~DIP~ґଘؔٯసͷݪଇ (ݴ͍͑ྫ) υϝΠϯͷநΠϯϑϥετϥΫνϟͷ࣮ͷৄ ࡉʹґଘͯ͠ͳΒͳ͍ɻ ΠϯϑϥετϥΫνϟͷ࣮ͷৄࡉ͕υϝΠϯͷந ʹґଘ͖͢Ͱ͋Δɻ >ʮநʯ࣮ͷৄࡉʹґଘͯ͠ͳΒͳ͍ɻ࣮ͷ ৄࡉ͕ʮநʯʹґଘ͖͢Ͱ͋Δɻ
Layered Architecture(DIP)αϯϓϧ ίʔυ(؆қ) ! https://github.com/kimutyam/layered-dip-hands-on
ύοέʔδྫ ! ϨΠϠຖʹϓϩδΣΫτׂ
υϝΠϯϓϩδΣΫτ(ྫ) ! RepositoryͷI/FΛఆٛ
ΠϯϑϥετϥΫνϟϓϩδΣΫτ (ྫ) ! Repositoryͷ࣮
ٕज़͕૿͍͑ͯ͘ʹͭΕͯΠϯϑϥετϥΫνϟ͕ ϑΝοτʹͳΓ͕ͪ(ΠϯϑϥετϥΫνϟΛϓϩδΣΫ τׂͨ͠Βղܾ) UI͕૿͍͑ͯ͘ʹͭΕͯUI͕ϑΝοτʹͳΓ͕ͪ(UIΛ ϓϩδΣΫτׂͨ͠Βղܾ) Layered Architecture With DIP ΞʔΩςΫνϟϨϕϧͰΠϯϑϥετϥΫνϟͱUIͷ
ؔ৺ͷΛߦ͏͜ͱ͕͍͠
2.ϔΩαΰφϧΞʔΩςΫνϟ֓ཁ
ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture༁)
ଆͱ֎ଆͷΞμϓλ ΞϓϦέʔγϣϯͷ֎ଆ ! ΞϓϦέʔγϣϯػೳཁ ݅ υϝΠϯͷϏδωε ϩδοΫ ଆͱ֎ଆͷίϛϡχέʔγϣ ϯΛࣝผ͢Δϙʔτ
ґଘؔਤ
ॲཧͷྲྀΕ
Hexagonal Architecture༻ޠ(1) • Port(ϙʔτ) ΠϯλʔϑΣʔεΛجૅʹσόΠε(αʔϏε)ؒͷձ Λࣝผ͢Δɻϙʔτͷઃܭऀ࣍ୈͰࣗ༝ɻ >ͳʹ͕ϙʔτͰɺͳʹ͕ͦ͏Ͱͳ͍͔ɺ΄ͱΜͲ Έͷͩɻ >Θͨ͠ͷબɺ2,3,4ϙʔτͷখ͍͞ࣈΛΉ ͕͋Δɻ
ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture༁)
Hexagonal Architecture༻ޠ(2) • Adapter(Ξμϓλ) ΠϯλʔϑΣʔεΛ֤σόΠε(αʔϏε)͕ඞཁͱ͢Δ ৴߸ʹม͑Δ Port : Adapter =
1 : N >σόΠε͕ඞཁͱ͢Δ৴߸ʹม͑Δɺٯ·ͨવΓɻ ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture༁)
Hexagonal Architecture༻ޠ(3) • Primary Port(ϓϥΠϚϦʔϙʔτ) ΞϓϦʔέʔγϣϯΛۦಈ͢Δϙʔτͷ͜ͱΛࢦ͢
Hexagonal Architecture༻ޠ(4) • Secondary Port(ηΧϯμϦʔϙʔτ) ΞϓϦʔέʔγϣϯʹۦಈ͞ΕΔϙʔτͷ͜ͱΛࢦ͢
Hexagonal Architectureͷࢫຯ(1) • ϙʔτ୯ҐͰؔ৺Λ͢Δ͜ͱ͕Մೳ • υϝΠϯΛத৺ʹஔ͘ࣄͰΞϓϦέʔγϣϯશମͷํ ͕ܾఆ͞ΕΔ ϙʔτؒͰͷґଘͳ͘ɺϙʔτ্ͰυϝΠϯͳ͍͠ ΞϓϦέʔγϣϯͷ࣮Λར༻ͨ͠ΓɺI/FΛ࣮͢Δ •
Ξμϓλ୯ҐͰٕज़ͷΈସ͕͑Մೳ(ৄࡉͷٕज़Λมߋ ͯ͠υϝΠϯʹӨڹ͠ͳ͍) • ϞοΫԽ͍͢͠(ϞοΫDBΛར༻͢ΔΑ͏ͳΈ͕ ؆୯ʹ࣮ݱͰ͖Δ) • ࢹ֮తͰॲཧͷϑϩʔґଘ͕ؔ໌ྎ
Hexagonal Architectureαϯϓϧ ίʔυ https://github.com/kimutyam/hexagonal-hands-on
ύοέʔδྫ ྫʹΑͬͯϨΠϠɾϙʔτ ຖʹϓϩδΣΫτׂ
υϝΠϯϓϩδΣΫτྫ(1) • ίΞυϝΠϯͱ αϒυϝΠϯΛ ू • ڥք͚ͮΒΕͨ ίϯςΩετؒ ͷґଘΛͳ͘͢ •
υϝΠϯҎ֎ͷ ϓϩδΣΫτͷ ґଘͤ͞ͳ͍
υϝΠϯϓϩδΣΫτྫ(2) • υϝΠϯϞσϧ (Entity,VO,Service,Event,Mo dule) • Repository,Factory,Aggrega te etc
υϝΠϯϓϩδΣΫτྫ(2)
ΞϓϦέʔγϣϯϓϩδΣΫτྫ(1) ΞϓϦέʔγϣϯϓϩδΣΫτ ֤υϝΠϯΛґଘͤ͞Δ
ΞϓϦέʔγϣϯϓϩδΣΫτྫ(2) • ΞϓϦέʔγϣϯαʔϏε ࠞཚ͕ͪ͠ͳαʔϏεͱ͍͏֓೦ʹ͍ͭͯ υϝΠϯαʔϏεͱΞϓϦέʔγϣϯαʔϏεͷҧ͍ ʹ͍ͭͯҎԼࢀর
ΞϓϦέʔγϣϯϓϩδΣΫτྫ(3) ڥք͚ͮΒΕͨυϝΠϯίϯ ςΩετͷConverter(Mapper)
ΞϓϦέʔγϣϯϓϩδΣΫτྫ(4) DTO(Data Transfer Object)
ϙʔτϓϩδΣΫτྫ(1) • ϙʔτϓϩδΣΫ τ ϓϥΠϚϦʔϙʔ τͱηΧϯμϦϙʔ τΛू • ϓϥΠϚϦʔϙʔ τ
• ηΧϯμϦʔϙʔ τ ! ※ϙʔτؒͷґଘ ͳ͠
ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (1) • WebServiceϙʔτ • HttpʹΑͬͯۦಈ͞ΕΔ • PlayFrameworkΛར༻
ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (2) • WebService ϙʔτ • Controll er • Router
• json • Form
ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (3) • Batchϙʔτ • CLIͰ࣮ߦ͢Δ
ϓϥΠϚϦʔϙʔτϓϩδΣΫτྫ (4) • Batchϙʔτ • Batch • Scheduler etc…
• Persistenceϙʔτ • RDBMSKVSͷӬଓԽ ͷ ηΧϯμϦϙʔτϓϩδΣΫτྫ(1) • ThirdPartyAPIϙʔτ • ThirdPartyͷAPIΛܦ༝͠
ͨίϛϡχέʔγϣϯΛ ͢Δ
• Persistenceϙʔτ • RDBMSKVSͷ۩ମతͳ࣮ • υϝΠϯϦϙδτϦI/Fͷํʹै͏ • DAO ηΧϯμϦϙʔτϓϩδΣΫτྫ(2)
• ThirdPartyAPIϙʔτ • APIClient • υϝΠϯϦϙδτϦI/Fʹ ै࣮ͬͨ ηΧϯμϦϙʔτϓϩδΣΫτྫ(3)
• ֤ϓϩδΣΫτΛूɾґଘ • ·ͱΊ RootϓϩδΣΫτྫ(1)
• DependencyInjection • ΞϓϦέʔγϣϯͷઃఆϑΝ Πϧ RootϓϩδΣΫτྫ(2)
Hexagonal Architectureͷࢫຯ(2) ϔΩαΰφϧΞʔΩςΫνϟෳͷ֎෦αʔϏεͱͷ࿈ ܞ͕࣮͍͢͠ ํத৺ʹυϝΠϯΛਾ͑Δɹˠɹ౷ҰతͳઃܭΛҡ ࣋͢Δ͜ͱ͕Մೳ
3.ฐࣾͰͷιϑτΣΞઓུ
ฐࣾͰߦ͍ͬͯΔઓུ ઓུతυϝΠϯ ઃܭ ઓज़తυϝΠϯ ઃܭ ৄࡉ࣮ ϞοΫ࣮ ϞϊϦγοΫΞϓϦέʔγϣϯ
ฐࣾͰߦ͍ͬͯΔઓུ ઓུతυϝΠϯ ઃܭ ઓज़తυϝΠϯ ઃܭ ৄࡉ࣮ ϞοΫ࣮ ઓུతυϝΠϯ ઃܭ ઓज़తυϝΠϯ
ઃܭ ৄࡉ࣮ ϞοΫ࣮ ڥք͚ͮΒΕͨίϯςΩετ୯ҐͰϚ ΠΫϩαʔϏεԽ
ϚΠΫϩαʔϏεΞʔΩςΫνϟͷΠϝʔδ !
࣮ફೖͯ͠ΈͯͷϝϦοτ·ͱΊ • ϓϩδΣΫτɾϓϩμΫτಋೖظͷυϝΠϯΛৠཹͤ͞ ͳ͕ΒɺϞοΩϯά͕͍͢͠ • ґଘؔͷ੍͕ڧ͍ΞʔΩςΫνϟͳͷͰίʔυͷํ ͕ͿΕͮΒ͍ • ৽͍ٕ͠ज़Λಋೖ͍͢͠ •
ϚΠΫϩαʔϏεͱͷ૬ੑ͕Α͍ • ϨΠϠʔυΞʔΩςΫνϟDIPͱൺͯؔ৺ͷ͕͠ ͘͢ • ΞʔΩςΫνϟਤͷࢹ֮ޮՌʹΑΓϝϯόʔͷཧղ͕ ͍ ! !
࣮ફೖͯ͠ΈͯΜͩ͜ͱ·ͱΊ • Batchͷ։ൃΛBatchϙʔτͰߦ͍͕ͬͯͨɺ ඞͣ͠υϝΠϯΛܦ༝͢ΔBatchͰͳ͔ͬͨɻ (MySQLͷύʔςΟγϣϯՃͱ͔) →ɹRootϓϩδΣΫτͷઃఆͱͯ͠ఆ͍ٛͯ͠Δ →ɹͦΕΑ͏ͷϓϩδΣΫτΛΔͷ͋Γͩͱߟ͑Δ ! • ϙʔτؒͷίϛϡχέʔγϣϯ͕Ͱ͖ͳ͍ͷͰɺ
ͪΐͬͱ࣮ͨ͠Λॻ͘ͱ͖ͰυϝΠϯܦ༝Ͱ࣮ݱΛߟ ͑Δඞཁ͕͋ͬͨ →ɹ͍͍ҙຯͰѱ͍ҙຯͰ੍͕͋ΓɺΞτϓοτ ͕͘ͳΔέʔε͕͋Γ·͢ɻ׳ΕΕͳΜͯ͜ͱͳ͍Ͱ ͕͢ɻ
ࢀߟࢿྉ ॻ੶ ʰ࣮ફυϝΠϯۦಈઃܭ (Object Oriented SELECTION)ʱ ॻ੶ʰΤϦοΫɾΤϰΝϯεͷυϝΠϯۦಈઃܭʱ ϔΩαΰφϧΞʔΩςΫνϟͱϨΠϠʔΞʔΩςΫνϟͷҧ͍ͱϚΠΫϩαʔϏε - falsandtru
ͷϝϞா υϝΠϯʹ࠷దͳΞʔΩςΫνϟΛߟ͑Δ ϔΩαΰφϧΞʔΩςΫνϟ(Hexagonal architecture༁) ΦϒδΣΫτࢥߟ: ґଘؔٯసͷݪଇ ࠞཚ͕ͪ͠ͳαʔϏεͱ͍͏֓೦ʹ͍ͭͯ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠