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
ROM.rb: Another Approach to Persistence
Search
Seiya IZUMI
September 28, 2017
Programming
1
840
ROM.rb: Another Approach to Persistence
Meguro.rb #7
Seiya IZUMI
September 28, 2017
Tweet
Share
More Decks by Seiya IZUMI
See All by Seiya IZUMI
Elm, the functional frontend
izumisy
3
1k
Elmの歩き方2019
izumisy
5
3.3k
Our Journey with the Biggest Elm App in Japan
izumisy
0
140
jQueryからElmまで
izumisy
1
1.6k
Ordering and Ordered
izumisy
1
73
僕が新卒として意識している4つのこと
izumisy
0
100
Choo: Fun Functional Framework
izumisy
1
450
StackoverflowでREPを稼ぐ技術
izumisy
1
770
フロントエンド・バリデーション
izumisy
5
3.5k
Other Decks in Programming
See All in Programming
Quine, Polyglot, 良いコード
qnighy
4
640
Jakarta EE meets AI
ivargrimstad
0
160
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
Jakarta EE meets AI
ivargrimstad
0
610
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
24k
ペアーズにおけるAmazon Bedrockを⽤いた障害対応⽀援 ⽣成AIツールの導⼊事例 @ 20241115配信AWSウェビナー登壇
fukubaka0825
6
1.9k
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
110
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
A Journey of Contribution and Collaboration in Open Source
ivargrimstad
0
910
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Featured
See All Featured
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.2k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Happy Clients
brianwarren
98
6.7k
Visualization
eitanlees
145
15k
Building Your Own Lightsaber
phodgson
103
6.1k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Statistics for Hackers
jakevdp
796
220k
Scaling GitHub
holman
458
140k
YesSQL, Process and Tooling at Scale
rocio
169
14k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Transcript
ROM.rb: Another Approach to Persistence Seiya IZUMI (@sy_izumi)
About me • ଂେֶ4 • 1ؒٳֶͯ͠ITاۀͰΊͪΌͪ͘ΌΠϯλʔ ϯͨ͠Γͨ͠ • ීஈVue.jsReactNativeͳͲΛ৮ͬͯ·͢ @sy_izumi
IzumiSy
ࠓROM.rbͷͳ͠Λ͠·͢
RailsΞϓϦέʔγϣϯΛ࡞͍ͬͯͯ • Model͕σʔλϕʔεʹର͢Δૢ࡞ͱɺυϝΠ ϯର͢Δૢ࡞ͱ͍͏̎ͭͷΛ͕࣋ͪͪʹ ͳΔ͜ͱ͕ଟ͍ • Ϟσϧ͕࣮࣭ɺӬଓԽϨΠϠͱૄ݁߹ database schema ≒
domain model
ActiveRecordͷ͍ͤͰͳ͍ • ActiveRecordύλʔϯʹ͓͍ͯɺϞσϧ͕ ӬଓԽϨΠϠͷߏͱಉ͡Ͱ͋Δͱ͍͏͜ͱ རɻ • ϦϑΝΫλϦϯάύλʔϯͳͲʹΑͬͯదͳ ରॲ͕Ͱ͖Δ (FormObject, Validator,
Callback,etc …)
ʮAR͡Όͳ͍ͱμϝʯͰͳ͍ • ϦϑΝΫλϦϯάύλʔϯΛۦͯ͠ ActiveRecordΛ͍͜ͳ͢ͷͻͱͭͷબ • Ͱɺ͔ͨ͠͠ΒସʹͳΔΑ͏ͳҟͳΔ ࢥͷORϚούʔ͕͋Δ͔ʂ • άάͬͯΈΔ
͋ͬͨ
ROM.rbͱ • RubyΞϓϦέʔγϣϯͷͨΊͷӬଓԽϥΠϒ ϥϦ • ୯ͳΔORϚούʔͰͳ͘ɺ༷ʑͳӬଓԽ ஔΛϓϥΨϒϧͳΞμϓλͱͯ͑͠Δ • Separating persistence
from domain layer
ϔΩαΰφϧɾΞʔΩςΫνϟ http://blog.tai2.net/hexagonal_architexture.html
ӬଓԽΛͲ͏ଊ͑Δ͔ʁ User.create(name: “Bob”) POST https://api.stripe.com/v1/charges
ӬଓԽϨΠϠͱૄ݁߹ੑ • ΞϓϦέʔγϣϯͷ֎෦ʹ͋Δஔͷૢ࡞ํ ๏ʹؔͯ͠Ͱ͖Δ͚ͩૄ݁߹ʹ͢Δ • WebAPI͍ͭ·Ͱ͑ΔͱݶΒͳ͍ ʢcf. WebPayͷαʔϏεऴྃ in 2016ʣ
• ΞϓϦέʔγϣϯ͕ϏδωεҎ֎ͷͷʹґ ଘ͠ॊೈੑ͕ࣦΘΕΔͷΛආ͚Δ
How to use ROM.rb?
Architecture .PEFM 3FQPTJUPSZ 3FMBUJPO "EBQUFS Infratructure Layer Domain Layer
Architecture 1030 ROM::Repository ROM::Relation ROM::Gateway Infratructure Layer Domain Layer
Model module Domain class Book attr_reader :id, :title, :price def
initialize(attrs) @id, @title, @price = attrs.values_at(:id, :title, :price) end end end • POROͳΞϓϦέʔγϣϯݻ༗ͷϩδοΫΛ࣋ͭ • ଟ͘ͷ߹RepositoryʹΑͬͯੜ͞ΕΔ • ROM.rb͕͜͜ʹ༩͑ΔӨڹ࠷খݶ
Repository module Repository class Book < ROM::Repository[:books] def by_id(id) books.by_pk(id).map_to(Domain::Books).one!
end def all books.map_to(Domain::Book) end end end • υϝΠϯϨΠϠͱӬଓԽϨΠϠͷ૭ޱʹͳΔ෦ • υϝΠϯʹదͳΦϒδΣΫτΛੜ͠ɺӬଓԽஔʹݻ༗ͳΫΤϦੜ ͳͲRelation͕ͱ͚ͯ͠ෛ͏
Relation class Books < ROM::Relation[:sql] schema(infer: true) do associations do
belongs_to :author end def by_id(id) where(id: id) end def available where(is_available: true) end end end • ӬଓԽϨΠϠͱͯ͠Ξμϓλʹ ର͢Δૢ࡞Λϥοϓ͢Δ • ΞιγΤʔγϣϯϑΟʔϧυ όϦσʔγϣϯͳͲΛ͜͜Ͱఆ ٛ • ରͱͳΔΞμϓλʹݻ༗ͷૢ ࡞͕Ͱ͖Δ • ͜͜Ͱఆٛͨ͠ϝιου܈Λ RepositoryͰ͑Δ
Adapter • ӬଓԽஔͷ۩ମతͳૢ࡞Λߦ͏ • 3rd-partyͷओཁͳAPIΞμϓλ͍͔ͭ͘༻ҙ͞Ε͍ͯΔ • SQLΞμϓλʹSequel͕ϥοϓ͞Ε͍ͯΔ
·ͱΊ
·ͱΊ • ROM.rbӬଓԽϨΠϠͷநԽͱυϝΠϯϨ ΠϠ͔ΒͷΛΨονϦͱαϙʔτͯ͘͠ ΕΔ • υϝΠϯʢϞσϧʣϨΠϠͱӬଓԽϨΠϠΛ ૄ݁߹ʹͯ͠ɺΞϓϦέʔγϣϯΛมԽʹڧ ͘͠Α͏
Ұํ·ͨผͷҙݟ • ͲͷΛͲ͏͚Δ͔ͱ͍͏؍ͷ౷Ұ͕ ग़དྷ͍ͯͳ͍ͱӡ༻ͣ͠Β͍ݒ೦͕͋Δ • ιʔείʔυ͕ࢄΒͬͯݟ௨͕͠ѱ͘ͳΔͱ ͍͏ਓ͍Δ • ΞϓϦέʔγϣϯͷఆنʹ߹Θͤͯద ͳπʔϧΛબͿͷ͕BEST
https://github.com/IzumiSy/sample_bookshelf
Thank You! http://rom-rb.org