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
970
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
55
devsumi-2024-summer
onigra
4
1.6k
第一種低層住居専用地域
onigra
0
220
jaws-ug-ecspresso-meetup-20230808
onigra
0
1.6k
PHP-CS-FixerとかAtomとか
onigra
1
1.3k
プログラミング初心者でも始められるコミュニティへの参加と貢献
onigra
4
730
Techblog Deep Dive Meetup #1
onigra
0
1.8k
とある業務オペレーション自動化の話
onigra
0
950
about tsudura
onigra
0
240
Other Decks in Technology
See All in Technology
ABEMA のコンテンツ制作を最適化!生成 AI x クラウド映像編集システム / abema-ai-editor
cyberagentdevelopers
PRO
1
180
話題のGraphRAG、その可能性と課題を理解する
hide212131
4
1.5k
カメラを用いた店内計測におけるオプトインの仕組みの実現 / ai-optin-camera
cyberagentdevelopers
PRO
1
120
GitHub Universe: Evaluating RAG apps in GitHub Actions
pamelafox
0
170
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
5
49k
AWSコンテナ本出版から3年経った今、もし改めて執筆し直すなら / If I revise our container book
iselegant
15
4k
【若手エンジニア応援LT会】AWSで繋がり、共に成長! ~コミュニティ活動と新人教育への挑戦~
kazushi_ohata
0
180
LeSSに潜む「隠れWF病」とその処方箋
lycorptech_jp
PRO
2
120
Forget efficiency – Become more productive without the stress
ufried
0
120
WINTICKETアプリで実現した高可用性と高速リリースを支えるエコシステム / winticket-eco-system
cyberagentdevelopers
PRO
1
190
Shift-from-React-to-Vue
calm1205
3
1.3k
【若手エンジニア応援LT会】AWS Security Hubの活用に苦労した話
kazushi_ohata
0
160
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
229
18k
How to Think Like a Performance Engineer
csswizardry
19
1.1k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
BBQ
matthewcrist
85
9.3k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
4 Signs Your Business is Dying
shpigford
180
21k
Practical Orchestrator
shlominoach
186
10k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Designing the Hi-DPI Web
ddemaree
280
34k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
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ϚδͰ͍͢͝…
ฏຌͳ݁Ͱ͕͢ • ͲͪΒͷํ͕༏Ε͍ͯΔͱ͍͏Ͱͳ͘ɺ ϑϨʔϜϫʔΫΞʔΩςΫνϟͷࢥͱ ϢʔεέʔεΛཧղ͠ɺదͳٕज़બఆΛͯ͠ ͍͖·͠ΐ͏
ҟͳΔݴޠͷϑϨʔϜϫʔΫ ઃܭࢥʹ৮ΕΔ͜ͱɺ ઃܭͷ෯Λ͛Δ͜ͱʹͭͳ͕Δ