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
カラム追加で増えるActiveRecordのメモリサイズ イメージできますか?
asayamakk
4
1.6k
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
170
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
CSC509 Lecture 09
javiergs
PRO
0
110
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
3
400
飲食業界向けマルチプロダクトを実現させる開発体制とリアルな現状
hiroya0601
1
390
Nuxtベースの「WXT」でChrome拡張を作成する | Vue Fes 2024 ランチセッション
moshi1121
1
520
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
920
Modern Angular: Renovation for Your Applications
manfredsteyer
PRO
0
210
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
230
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
1
290
RailsのPull requestsのレビューの時に私が考えていること
yahonda
5
1.7k
Featured
See All Featured
For a Future-Friendly Web
brad_frost
175
9.4k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.4k
We Have a Design System, Now What?
morganepeng
50
7.2k
Being A Developer After 40
akosma
86
590k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
9
680
BBQ
matthewcrist
85
9.3k
GraphQLとの向き合い方2022年版
quramy
43
13k
The Art of Programming - Codeland 2020
erikaheidi
51
13k
The World Runs on Bad Software
bkeepers
PRO
65
11k
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