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
ginza-ruby-kaigi-01
Search
onigra
August 05, 2017
Technology
4
980
ginza-ruby-kaigi-01
https://ginzarb.github.io/kaigi01/
onigra
August 05, 2017
Tweet
Share
More Decks by onigra
See All by onigra
THE GOAL
onigra
3
67
devsumi-2024-summer
onigra
4
1.9k
第一種低層住居専用地域
onigra
0
230
jaws-ug-ecspresso-meetup-20230808
onigra
0
1.7k
PHP-CS-FixerとかAtomとか
onigra
1
1.3k
プログラミング初心者でも始められるコミュニティへの参加と貢献
onigra
4
740
Techblog Deep Dive Meetup #1
onigra
0
1.9k
とある業務オペレーション自動化の話
onigra
0
960
about tsudura
onigra
0
250
Other Decks in Technology
See All in Technology
開発生産性向上! 育成を「改善」と捉えるエンジニア育成戦略
shoota
1
230
宇宙ベンチャーにおける最近の情シス取り組みについて
axelmizu
0
110
Snykで始めるセキュリティ担当者とSREと開発者が楽になる脆弱性対応 / Getting started with Snyk Vulnerability Response
yamaguchitk333
2
180
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
300
サイボウズフロントエンドエキスパートチームについて / FrontendExpert Team
cybozuinsideout
PRO
5
38k
OpenShift Virtualizationのネットワーク構成を真剣に考えてみた/OpenShift Virtualization's Network Configuration
tnk4on
0
130
podman_update_2024-12
orimanabu
1
260
バクラクのドキュメント解析技術と実データにおける課題 / layerx-ccc-winter-2024
shimacos
2
1k
コンテナセキュリティのためのLandlock入門
nullpo_head
2
320
re:Invent 2024 Innovation Talks(NET201)で語られた大切なこと
shotashiratori
0
300
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
2
2.1k
Wvlet: A New Flow-Style Query Language For Functional Data Modeling and Interactive Data Analysis - Trino Summit 2024
xerial
1
110
Featured
See All Featured
GraphQLとの向き合い方2022年版
quramy
44
13k
Six Lessons from altMBA
skipperchong
27
3.5k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Agile that works and the tools we love
rasmusluckow
328
21k
Building Your Own Lightsaber
phodgson
103
6.1k
We Have a Design System, Now What?
morganepeng
51
7.3k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
48
2.2k
Navigating Team Friction
lara
183
15k
Embracing the Ebb and Flow
colly
84
4.5k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
4 Signs Your Business is Dying
shpigford
181
21k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Transcript
Spring Frameworkͱൺֱֶͯ͠Ϳɺ WebΞϓϦέʔγϣϯϑϨʔϜϫʔΫͷ ׂ ͗Μ͟Rubyձٞ 01 2017-08-05 @onigra_
Yudai Suzuki / ླ༤େ @onigra_ onigra
͜ͷΛ͢Δʹ͋ͨͬͯ • ϓϩάϥϚʔͱͯ͠ࣄ͢Δ্Ͱɺ ॳΊͯͪΌΜͱֶΜͩͷ͕Ruby • ࣄͰ͏Web Application Frameworkɺ ΄ͱΜͲRailsͩͬͨ
Railsʹֶ͍ͭͯΜͰߦ͘͏ͪʹ • ใΛूΊͯΈΔͱɺRailsʹׂͱ ൷͕ଟ͍͜ͱʹؾͮ͘
ྫ • MVCͷఆٛʹ͍ͭͯ • ActiveRecord
ͳΔ΄Ͳ
ࠓͷ4݄ʹస৬
http://rubykaigi.org/2017/sponsors • http://rubykaigi.org/2017/sponsors
స৬ͯ͠ Java + Spring Frameworkʹ৮ΕΔ • ਖ਼֬ʹSpring Boot • Spring
BootɺSpring Frameworkͷ͍ͭ͘ ͔ͷϞδϡʔϧΛ౷߹ͯ͠ɺWebΞϓϦ έʔγϣϯΛߏஙͨ͘͢͠͠ͷ
Spring FrameworkΛ௨͠ ൷ͷ༰Λ վΊͯཧղ͢Δ
͜ͷൃදͷதͰ͍͑ͨ͜ͱ • ࢲͷΑ͏ʹɺϓϩάϥϛϯάWeb։ൃΛ RubyɺRails͔Β͡Ίͨํʑʹɺ ҟͳΔݴޠͷϑϨʔϜϫʔΫͷ ઃܭࢥύϥμΠϜʹ৮ΕΔ͜ͱʹΑΓɺ ৽͍͠ݟΛಘΔػձʹͳ͍͚ͬͯͨͩΕ ͍Ͱ͢
ҙ • ຊൃදͰপʹೖΓࠐΉͷΛආ͚ΔͨΊɺҎԼʹ͍ͭͯ ਂ͘౿ΈࠐΜͩ͠·ͤΜ • υϝΠϯۦಈઃܭ(DDD) • ΤϯλʔϓϥΠζΞϓϦέʔγϣϯΞʔΩςΫνϟύλʔ ϯ(PofEAA) •
͋͘·ͰRuby on RailsͱSpring Frameworkͷػೳͷൺֱͱ ͍͏؍Ͱ͓͠·͢
ࠂ
גࣜձࣾΤεΤϜΤεͰɺ ษڧձͷձఏڙΛ͡Ί·͢ • ࠷େͰ136໊·Ͱऩ༰ՄೳͳձΛఏڙͰ͖· ͢ • ৄࡉ@onigra_·Ͱ͓͍߹Θ͍ͤͩ͘͞ • ॴେ(ࣳެԂ) •
https://twitter.com/onigra_
Elastic Leadershipͷ ΠϕϯτΓ·͢ • ฐࣾͨͳ͢ͳ͓(@sunaot)͕ΤοηΠΛدߘ͠ ͨɺElastic LeadershipͷΠϕϯτΛߦ͍·͢ • ήετʹϨδϟϦʔϫʔΫε߹ಉձࣾͷ Shinichiro
OGAWA(@conceal_rs)͞ΜΛ͓ট͖͠ ·͢ • ͪ͜Βৄࡉ͕ܾ·Γ࣍ୈɺSNSʹͯࠂ͠·͢
σʔλΞΫηε
σʔλΞΫηε • Spring FrameworkʹɺRDBMSͷσʔλ ӬଓԽʹΞΫηε͢ΔͨΊͷϥΠϒϥϦͱ ͯ͠ɺSpring Data͕ଘࡏ͢Δ • Spring DataɺRDBMSҎ֎ͷσʔλιʔε
ʹରԠ࣮͕ͨ͠ଘࡏ͢Δ
Spring Data • https://projects.spring.io/spring-data/ • MongoDB • LDAP • ֎෦Web
API • Redis • Elasticsearch • etc…
Repository • Spring Core͕ఏڙ͍ͯ͠Δ • υϝΠϯΦϒδΣΫτͱσʔλΞΫηεͷ࣮Λ ্ͨ͠ͰɺRepositoryͦΕΒͷհΛߦ͏ • Spring Core͕ఏڙ͢ΔRepositoryʹର͠ɺ
Spring DataͷΛTemplateͱݺͿ
ϑϨʔϜϫʔΫͱͯ͠ͷ ActiveRecordͷσϝϦοτ • ϞσϧͱDB͕ີ݁߹ • Modelͱσʔλϕʔεઃܭ͕݁߹ͯ͠͠·͏ • ΦϒδΣΫτ͕σʔλϕʔεςʔϒϧͱରԠ͠ ͍ͯΔͱActiveRecordඇৗʹ༗ޮ •
σʔλϕʔεͷઃܭ͕ɺ͍͔ʹActiveRecordͰѻ͍ ͘͢͢ΔʹͲ͏͢Δ͔ɺͱ͍͏ํʹͳΓ͕ͪ
RepositoryͱTemplateΛ ༻͍࣮ͨ • Interfaceͷఆٛ • Interfaceʹର͠ɺσʔλιʔε͝ͱʹɺσʔ λΞΫηεͷ۩ମతͳ࣮Λߦ͏
RepositoryͱTemplateΛ ༻͍࣮ͨ
Կ͕خ͍͔͠ʁ • RepositoryͰఆٛͨ͠ΠϯλʔϑΣʔεͱɺ Templateͷ࣮Λ͚Δ͜ͱʹΑͬͯɺ ϏδωεϩδοΫͱ࣮ͷ͕Ͱ͖Δ • σʔλΞΫηεͷ࣮ΛΓସ͑Δ͚ͩͰɺΞϓ Ϧέʔγϣϯͷมߋ͕ྃ͢Δ • ϏδωεϩδοΫʹର࣮ͯ͠ͷมߋ͕༰қʹͳΔ
ActiveResource • ֎෦ͷWeb APIΛɺAcriveRecordΦϒδΣΫ τͷΑ͏ʹѻ͏͜ͱ͕Ͱ͖ΔΑ͏ʹͳΔgem • Rails2, 3ʹCoreʹ͕͋ͬͨɺ4Ͱ֎Εͨ
RubyͰRepository • rom-rb • http://rom-rb.org/ • Ruby Object Mapper •
HANAMIͷ෦Ͱ༻͞Ε͍ͯΔ • http://hanamirb.org/
Callbackͱ ΞεϖΫτࢦϓϩάϥϛϯά
ΞεϖΫτࢦϓϩάϥϛϯά ͱʁ • Aspect Oriented Programming • ΦϒδΣΫτͷ୯ҐͰ͏·͘Ͱ͖ͳ͍ɺ ΦϒδΣΫτؒΛ·͙ͨػೳΛʮہ໘ʯͱ͍͏ ୯Ґ͝ͱʹϞδϡʔϧԽ͢ΔϓϩάϥϛϯάύϥμΠ
Ϝ • ΦϒδΣΫτؒΛ·͙ͨػೳͷ͜ͱΛ ʮCrosscutting Concern(ԣஅతؔ৺ࣄ)ʯͱݺͿ
Crosscutting Concernͷྫ • Logging • Caching • Error Handling •
Transaction
ΞεϖΫτࢦϓϩάϥϛϯά Ͱ༻͍ΒΕΔ༻ޠ • Join Point • ΞϓϦέʔγϣϯ࣮ߦதͷɺ͋Δಛఆͷʮ࣌ʯΛࢦ͢ • Advice •
Join PointͰ࣮ߦ͞ΕΔॲཧ • Pointcut • Join Pointͷఆٛ
Spring AOP
Spring AOPͷ͍ํ • Join PointΛܾΊͯPointcutͱͯ͠ఆٛ͠ɺ PointcutͰߦ͏࣮ॲཧͰ͋ΔAdviceΛఆٛ͢ Δ • ಛఆͷΫϥεͷಛఆͷϝιου͕࣮ߦ͞ΕΔ લޙʹɺ࣮ߦ͢ΔॲཧΛఆٛ͢Δ
Pointcutͷఆٛ @Aspect public class RepositoryPointcut { // UserRepositoryͷsaveϝιου͕࣮ߦ͞ΕΔ࣌(Join Point)ΛPointcutͱͯ͠ఆٛ @Pointcut("execution(*
com.onigra.springaopsample.repository..UserRepository+.save(..))") public void save() {} }
Adviceͷఆٛ @Aspect @Component public class LoggingAdvice { private static final
Logger LOGGER = LoggerFactory.getLogger(LoggingAdvice.class); @Before(“com.onigra.springaopsample.aspect.UserPointcut.save()") public void beforeSave(JoinPoint joinPoint) { LOGGER.info("UserRepositoryͷsaveϝιου࣮ߦલʹϩάΛग़ྗ͢Δ"); } }
RailsͰݴ͏ͱCallback͔ͳ
AOPͱCallback ࣅͯඇͳΔͷ
RailsͷCallback • ActiveSupport::Callbacks#run_callbacksʹ ϒϩοΫΛ͢͜ͱͰ࣮ݱ • Rubyͷݴޠͱͯ͠ͷػೳΛར༻͍ͯ͠Δ
Spring AOP • όΠτίʔυม • ίϯύΠϧޙͷόΠφϦΛվม͠ɺॲཧΛࠩ ͠ࠐΉ
TransactionͰͷAOP • RailsɺActiveRecord::Transactions.transactionʹϒϩοΫΛ͢ • Spring FrameworkɺTransactionΛ͔͚͍ͨ ϝιουʹର͠ɺTransactionalΞϊςʔγϣϯ(Ճใ)Λ༩ • RailsͷΑ͏ʹɺϓϩάϥϜͷதͰ੍ޚΛߦ͏ํ๏ʹର͠ɺʮಛఆ ͷϝιουͷ࣮ߦ࣌ʹτϥϯβΫγϣϯΛ͔͚Δʯͱ͍͏
એݴΛߦ͏͜ͱΛɺʮએݴతτϥϯβΫγϣϯʯͱݺͿ • ※ϓϩάϥϛϯάʹΑΔτϥϯβΫγϣϯཧͰ͖Δ
MVCͱ ϨΠϠʔυΞʔΩςΫνϟ
ΞϓϦέʔγϣϯͷ ϨΠϠʔԽͷͱ MVC(MVC2, MVC Model2)ͷ શ͘ผͷʂʂʂ
Railsͷׂͷɺ ΞϓϦέʔγϣϯͷϨΠϠʔԽͷͱɺ MVCͷ͕ࠞͬͯ͟͠·͍ͬͯΔ ʢ͜ͱ͕ଟ͍ؾ͕͢Δʣ
͞Βʹͦ͜ʹDDDʹ͓͚ΔαʔϏεͱ PofEAAͷαʔϏεͷ͕ࠞͬͯ͟ େมͳ͜ͱʹ…
ΞϓϦέʔγϣϯઃܭʹ͓͚Δ ϨΠϠͷ֓೦ͱ • ݩClient-Server Modelͷߟ͑ํ • Webʹߟ͑ํ͕Ԡ༻͞Εɺൃల͖ͯͨ͠ • ͔͠͠ɺΞʔΩςΫνϟจݙʹΑ༷ͬͯʑ ͳϨΠϠʔͷ໊শ͕ଘࡏ͢ΔͷͰҙ͕ඞཁ
ΫϥΠΞϯταʔόϞσϧʹ͓ ͚Δ3ΞʔΩςΫνϟ • ϓϨθϯςʔγϣϯ: UI • ΞϓϦέʔγϣϯ: ϏδωεϩδοΫ • σʔλ:
σʔλΞΫηε
PofEAAʹ͓͚Δ3ΞʔΩςΫ νϟ • ϓϨθϯςʔγϣϯ: UI • υϝΠϯ: ϏδωεϩδοΫ • σʔλιʔε:
σʔλΞΫηε
͜ͷجຊͱͳΔ3Ͱ ߟ͑ͯݟΔ
Spring Framework • ϓϨθϯςʔγϣϯ • View(Template Engine(ThymeLeaf)) • Controller(Spring MVC)
• υϝΠϯ • Service(Spring Core) • Repository(Spring Core) • σʔλιʔε • Template(Spring Data) • ͦΕͧΕͷ͔ΒModelΛѻ͏
Spring Framework https://yo1000.gitbooks.io/self-study-spring/content/chapter1.html
Ruby on Rails • ϓϨθϯςʔγϣϯ • View(ActionView, View Template Engine(ERB))
• Controller(ActionController) • υϝΠϯ • Model(ActiveRecord, ActiveModel) • σʔλιʔε • Model(ActiveRecord)
༷ʑͳϨΠϠʔԽͷύλʔϯ͕ ଘࡏ͢Δ • ΞϓϦέʔγϣϯʹΑͬͯɺదͳϨΠϠԽ ҟͳΔ • จ຺ΞʔΩςΫνϟʹΑͬͯɺ໊শ͕ඃͬ ͨΓҟͳͬͨΓ૿͑ͨΓ͢Δͷʹҙ • ϨΠϠͷҙຯΛཧղ͠ɺదͳϨΠϠΛબ
͠Α͏
·ͱΊ
2ͭͷϑϨʔϜϫʔΫΛ ൺֱͯ͠ • RailsϢʔεέʔεΛߜͬͯɺׂΓͬͨઃܭʹ͍ͯ͠Δ • ͦͷɺϚον͠ͳ͍Ϗδωεཁ݅ʹΑͬͯɺਏΈવग़Δ • Spring Frameworkɺ͡Ί͔Β͍ϢʔεέʔεʹରԠ͢Δ ͜ͱΛࢦ͍ͯ͠Δ
• ෳࡶͳϏδωεཁ݅ɺظతͳ֦ுͷՄೳੑΛߟ͑Δͱɺ Spring Framework༏Εͨબࢶ • ҰํͰɺ͋Β͔͡Ίཧղ͓͖ͯ͘͠จ຺ɺ͕ࣝଟ͍
2ͭͷϑϨʔϜϫʔΫΛ ൺֱͯ͠ • ઃܭͷ෯Λ٘ਜ਼ʹͭͭ͠ɺ֮͑Δ͜ͱΛݮΒͯ͠։ ൃΛ্͛Δ͜ͱ͕Ͱ͖ΔActiveRecordΓศ ར • ໊শɺจ຺Λཧղ্ͨ͠ͰΞʔΩςΫνϟύλʔ ϯΛ͍͜ͳ͢ͷେม͔ͩΒɺେʹMVCͱ͍͏ ྨΛͨ͠ͷͰʁ
• ͦ͏ߟ͑ΔͱDHHϚδͰ͍͢͝…
ฏຌͳ݁Ͱ͕͢ • ͲͪΒͷํ͕༏Ε͍ͯΔͱ͍͏Ͱͳ͘ɺ ϑϨʔϜϫʔΫΞʔΩςΫνϟͷࢥͱ ϢʔεέʔεΛཧղ͠ɺదͳٕज़બఆΛͯ͠ ͍͖·͠ΐ͏
ҟͳΔݴޠͷϑϨʔϜϫʔΫ ઃܭࢥʹ৮ΕΔ͜ͱɺ ઃܭͷ෯Λ͛Δ͜ͱʹͭͳ͕Δ