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
830
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.5k
Ordering and Ordered
izumisy
1
71
僕が新卒として意識している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
Java ジェネリクス入門 2024
nagise
0
600
弊社の「意識チョット低いアーキテクチャ」10選
texmeijin
5
23k
Pinia Colada が実現するスマートな非同期処理
naokihaba
2
160
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
230
Vue.js学習の振り返り
hiro_xre
2
130
Kubernetes for Data Engineers: Building Scalable, Reliable Data Pipelines
sucitw
1
200
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
210
レガシーな Android アプリのリアーキテクチャ戦略
oidy
1
170
OpenTelemetryでRailsのパフォーマンス分析を始めてみよう(KoR2024)
ymtdzzz
4
1.6k
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
170
LLM生成文章の精度評価自動化とプロンプトチューニングの効率化について
layerx
PRO
2
140
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
240
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
72
5.3k
Code Reviewing Like a Champion
maltzj
519
39k
Typedesign – Prime Four
hannesfritz
39
2.4k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Thoughts on Productivity
jonyablonski
67
4.3k
A Modern Web Designer's Workflow
chriscoyier
692
190k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
Faster Mobile Websites
deanohume
304
30k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
92
16k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
Ruby is Unlike a Banana
tanoku
96
11k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
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