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
850
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
1.1k
Elmの歩き方2019
izumisy
5
3.3k
Our Journey with the Biggest Elm App in Japan
izumisy
0
150
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
780
フロントエンド・バリデーション
izumisy
5
3.5k
Other Decks in Programming
See All in Programming
なまけものオバケたち -PHP 8.4 に入った新機能の紹介-
tanakahisateru
1
120
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
300
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
340
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
290
fs2-io を試してたらバグを見つけて直した話
chencmd
0
240
Zoneless Testing
rainerhahnekamp
0
120
return文におけるstd::moveについて
onihusube
1
1.2k
Effective Signals in Angular 19+: Rules and Helpers @ngbe2024
manfredsteyer
PRO
0
140
Exploring: Partial and Independent Composables
blackbracken
0
100
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
210
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
150
ブラウザ単体でmp4書き出すまで - muddy-web - 2024-12
yue4u
3
490
Featured
See All Featured
Measuring & Analyzing Core Web Vitals
bluesmoon
4
170
Docker and Python
trallard
42
3.1k
Site-Speed That Sticks
csswizardry
2
190
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
2k
A Tale of Four Properties
chriscoyier
157
23k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
The Cult of Friendly URLs
andyhume
78
6.1k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
It's Worth the Effort
3n
183
28k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
What's in a price? How to price your products and services
michaelherold
243
12k
Code Reviewing Like a Champion
maltzj
521
39k
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