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
強いチームと開発生産性
onk
PRO
33
11k
元旅行会社の情シス部員が教えるおすすめなre:Inventへの行き方 / What is the most efficient way to re:Invent
naospon
2
330
Application Development WG Intro at AppDeveloperCon
salaboy
0
180
Incident Response Practices: Waroom's Features and Future Challenges
rrreeeyyy
0
160
テストコード品質を高めるためにMutation Testingライブラリ・Strykerを実戦導入してみた話
ysknsid25
7
2.6k
Making your applications cross-environment - OSCG 2024 NA
salaboy
0
180
社内で最大の技術的負債のリファクタリングに取り組んだお話し
kidooonn
1
550
スクラムチームを立ち上げる〜チーム開発で得られたもの・得られなかったもの〜
ohnoeight
2
350
RubyのWebアプリケーションを50倍速くする方法 / How to Make a Ruby Web Application 50 Times Faster
hogelog
3
940
いざ、BSC討伐の旅
nikinusu
2
780
IBC 2024 動画技術関連レポート / IBC 2024 Report
cyberagentdevelopers
PRO
0
110
rootlessコンテナのすゝめ - 研究室サーバーでもできる安全なコンテナ管理
kitsuya0828
3
380
Featured
See All Featured
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Git: the NoSQL Database
bkeepers
PRO
427
64k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
BBQ
matthewcrist
85
9.3k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
The Cost Of JavaScript in 2023
addyosmani
45
6.7k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
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༁) ΦϒδΣΫτࢥߟ: ґଘؔٯసͷݪଇ ࠞཚ͕ͪ͠ͳαʔϏεͱ͍͏֓೦ʹ͍ͭͯ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠