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.5k
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.7k
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
[JAWS-UG金沢支部×コンテナ支部合同企画]コンテナとは何か
furuton
3
250
LeSSに潜む「隠れWF病」とその処方箋
lycorptech_jp
PRO
2
120
Datachain会社紹介資料(2024年11月) / Company Deck
datachain
3
16k
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
290k
「 SharePoint 難しい」ってよく聞くけど、そんなに言うなら8歳の息子に試してもらった
taichinakamura
1
620
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
5
49k
GitHub Universe: Evaluating RAG apps in GitHub Actions
pamelafox
0
170
新R25、乃木坂46 Mobileなどのファンビジネスを支えるマルチテナンシーなプラットフォームの全体像 / cam-multi-cloud
cyberagentdevelopers
PRO
1
130
初心者に Vue.js を 教えるには
tsukuha
5
390
生成AIとAWS CDKで実現! 自社ブログレビューの効率化
ymae
2
330
チームを主語にしてみる / Making "Team" the Subject
ar_tama
4
310
コンテンツを支える 若手ゲームクリエイターの アートディレクションの事例紹介 / cagamefi-game
cyberagentdevelopers
PRO
1
130
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
243
12k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
14
1.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Designing on Purpose - Digital PM Summit 2013
jponch
115
6.9k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
Being A Developer After 40
akosma
86
590k
Speed Design
sergeychernyshev
24
570
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Testing 201, or: Great Expectations
jmmastey
38
7k
Building an army of robots
kneath
302
42k
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༁) ΦϒδΣΫτࢥߟ: ґଘؔٯసͷݪଇ ࠞཚ͕ͪ͠ͳαʔϏεͱ͍͏֓೦ʹ͍ͭͯ
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠