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.2k
Other Decks in Technology
See All in Technology
サイボウズフロントエンドエキスパートチームについて / FrontendExpert Team
cybozuinsideout
PRO
5
38k
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
500
ゼロから創る横断SREチーム 挑戦と進化の軌跡
rvirus0817
2
270
継続的にアウトカムを生み出し ビジネスにつなげる、 戦略と運営に対するタイミーのQUEST(探求)
zigorou
0
610
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
320
WACATE2024冬セッション資料(ユーザビリティ)
scarletplover
0
210
スタートアップで取り組んでいるAzureとMicrosoft 365のセキュリティ対策/How to Improve Azure and Microsoft 365 Security at Startup
yuj1osm
0
230
Fanstaの1年を大解剖! 一人SREはどこまでできるのか!?
syossan27
2
170
サイバー攻撃を想定したセキュリティガイドライン 策定とASM及びCNAPPの活用方法
syoshie
3
1.3k
DUSt3R, MASt3R, MASt3R-SfM にみる3D基盤モデル
spatial_ai_network
2
190
生成AIをより賢く エンジニアのための RAG入門 - Oracle AI Jam Session #20
kutsushitaneko
4
260
PHPからGoへのマイグレーション for DMMアフィリエイト
yabakokobayashi
1
170
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
243
12k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Being A Developer After 40
akosma
87
590k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
A designer walks into a library…
pauljervisheath
204
24k
For a Future-Friendly Web
brad_frost
175
9.4k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
Visualization
eitanlees
146
15k
A Tale of Four Properties
chriscoyier
157
23k
BBQ
matthewcrist
85
9.4k
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༁) ΦϒδΣΫτࢥߟ: ґଘؔٯసͷݪଇ ࠞཚ͕ͪ͠ͳαʔϏεͱ͍͏֓೦ʹ͍ͭͯ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠