ginza-ruby-kaigi-01

B8668563cbe87543c303dbb818cf9f02?s=47 onigra
August 05, 2017

 ginza-ruby-kaigi-01

B8668563cbe87543c303dbb818cf9f02?s=128

onigra

August 05, 2017
Tweet

Transcript

  1. Spring Frameworkͱൺֱֶͯ͠Ϳɺ
 WebΞϓϦέʔγϣϯϑϨʔϜϫʔΫͷ ੹຿෼ׂ ͗Μ͟Rubyձٞ 01 2017-08-05 @onigra_

  2. Yudai Suzuki / ླ໦༤େ @onigra_ onigra

  3. ͜ͷ࿩Λ͢Δʹ͋ͨͬͯ • ϓϩάϥϚʔͱͯ͠࢓ࣄ͢Δ্Ͱɺ
 ॳΊͯͪΌΜͱֶΜͩͷ͕Ruby • ࢓ࣄͰ࢖͏Web Application Framework͸ɺ ΄ͱΜͲRailsͩͬͨ

  4. Railsʹֶ͍ͭͯΜͰߦ͘͏ͪʹ • ৘ใΛूΊͯΈΔͱɺRailsʹ͸ׂͱ
 ൷൑͕ଟ͍͜ͱʹؾͮ͘

  5. ྫ • MVCͷఆٛʹ͍ͭͯ • ActiveRecord

  6. ͳΔ΄Ͳ

  7. ࠓ೥ͷ4݄ʹస৬

  8. http://rubykaigi.org/2017/sponsors • http://rubykaigi.org/2017/sponsors

  9. స৬ͯ͠
 Java + Spring Frameworkʹ৮ΕΔ • ਖ਼֬ʹ͸Spring Boot • Spring

    Boot͸ɺSpring Frameworkͷ͍ͭ͘ ͔ͷϞδϡʔϧΛ౷߹ͯ͠ɺWebΞϓϦ έʔγϣϯΛߏங͠΍ͨ͘͢͠΋ͷ
  10. Spring FrameworkΛ௨͠ ൷൑ͷ಺༰Λ
 վΊͯཧղ͢Δ

  11. ͜ͷൃදͷதͰ఻͍͑ͨ͜ͱ • ࢲͷΑ͏ʹɺϓϩάϥϛϯά΍Web։ൃΛ RubyɺRails͔Β͸͡Ίͨํʑʹɺ
 ҟͳΔݴޠͷϑϨʔϜϫʔΫͷ
 ઃܭࢥ૝΍ύϥμΠϜʹ৮ΕΔ͜ͱʹΑΓɺ ৽͍͠஌ݟΛಘΔػձʹͳ͍͚ͬͯͨͩΕ͹ ޾͍Ͱ͢

  12. ஫ҙ • ຊൃදͰ͸পʹೖΓࠐΉͷΛආ͚ΔͨΊɺҎԼʹ͍ͭͯ
 ਂ͘౿ΈࠐΜͩ࿩͸͠·ͤΜ • υϝΠϯۦಈઃܭ(DDD) • ΤϯλʔϓϥΠζΞϓϦέʔγϣϯΞʔΩςΫνϟύλʔ ϯ(PofEAA) •

    ͋͘·ͰRuby on RailsͱSpring Frameworkͷػೳͷൺֱͱ ͍͏؍఺Ͱ͓࿩͠·͢
  13. ࠂ஌

  14. גࣜձࣾΤεΤϜΤεͰ͸ɺ ษڧձͷձ৔ఏڙΛ͸͡Ί·͢ • ࠷େͰ136໊·Ͱऩ༰Մೳͳձ৔ΛఏڙͰ͖· ͢ • ৄࡉ͸@onigra_·Ͱ͓໰͍߹Θ͍ͤͩ͘͞ • ৔ॴ͸େ໳(ࣳެԂ) •

    https://twitter.com/onigra_
  15. Elastic Leadershipͷ Πϕϯτ΍Γ·͢ • ฐࣾͨͳ΂͢ͳ͓(@sunaot)͕ΤοηΠΛدߘ͠ ͨɺElastic LeadershipͷΠϕϯτΛߦ͍·͢ • ήετʹϨδϟϦʔϫʔΫε߹ಉձࣾͷ Shinichiro

    OGAWA(@conceal_rs)͞ΜΛ͓ট͖͠ ·͢ • ͪ͜Β΋ৄࡉ͕ܾ·Γ࣍ୈɺSNSʹͯࠂ஌͠·͢
  16. σʔλΞΫηε

  17. σʔλΞΫηε • Spring Frameworkʹ͸ɺRDBMS౳ͷσʔλ ӬଓԽ૚ʹΞΫηε͢ΔͨΊͷϥΠϒϥϦͱ ͯ͠ɺSpring Data͕ଘࡏ͢Δ • Spring Data͸ɺRDBMSҎ֎ͷσʔλιʔε

    ʹରԠ࣮ͨ͠૷͕ଘࡏ͢Δ
  18. Spring Data • https://projects.spring.io/spring-data/ • MongoDB • LDAP • ֎෦Web

    API • Redis • Elasticsearch • etc…
  19. Repository • Spring Core͕ఏڙ͍ͯ͠Δ • υϝΠϯΦϒδΣΫτͱσʔλΞΫηεͷ࣮૷Λ ෼཭্ͨ͠ͰɺRepository͸ͦΕΒͷ஥հΛߦ͏ • Spring Core͕ఏڙ͢ΔRepositoryʹର͠ɺ

    Spring Dataͷ੹຿ΛTemplateͱݺͿ
  20. ϑϨʔϜϫʔΫͱͯ͠ͷ ActiveRecordͷσϝϦοτ • ϞσϧͱDB͕ີ݁߹ • Modelͱσʔλϕʔεઃܭ͕݁߹ͯ͠͠·͏ • ΦϒδΣΫτ͕σʔλϕʔεςʔϒϧͱ௚઀ରԠ͠ ͍ͯΔͱActiveRecord͸ඇৗʹ༗ޮ •

    σʔλϕʔεͷઃܭ͕ɺ͍͔ʹActiveRecordͰѻ͍ ΍͘͢͢Δʹ͸Ͳ͏͢Δ͔ɺͱ͍͏ํ޲ʹͳΓ͕ͪ
  21. RepositoryͱTemplateΛ
 ༻͍࣮ͨ૷ • Interfaceͷఆٛ • Interfaceʹର͠ɺσʔλιʔε͝ͱʹɺσʔ λΞΫηεͷ۩ମతͳ࣮૷Λߦ͏

  22. RepositoryͱTemplateΛ
 ༻͍࣮ͨ૷

  23. Կ͕خ͍͔͠ʁ • RepositoryͰఆٛͨ͠ΠϯλʔϑΣʔεͱɺ Templateͷ࣮૷Λ෼͚Δ͜ͱʹΑͬͯɺ
 ϏδωεϩδοΫͱ࣮૷ͷ෼཭͕Ͱ͖Δ • σʔλΞΫηε૚ͷ࣮૷Λ੾Γସ͑Δ͚ͩͰɺΞϓ Ϧέʔγϣϯͷมߋ͕׬ྃ͢Δ • ϏδωεϩδοΫʹର࣮ͯ͠૷ͷมߋ͕༰қʹͳΔ

  24. ActiveResource • ֎෦ͷWeb APIΛɺAcriveRecordΦϒδΣΫ τͷΑ͏ʹѻ͏͜ͱ͕Ͱ͖ΔΑ͏ʹͳΔgem • Rails2, 3ʹ͸Coreʹ͕͋ͬͨɺ4Ͱ֎Εͨ

  25. RubyͰRepository • rom-rb • http://rom-rb.org/ • Ruby Object Mapper •

    HANAMIͷ಺෦Ͱ࢖༻͞Ε͍ͯΔ • http://hanamirb.org/
  26. Callbackͱ
 ΞεϖΫτࢦ޲ϓϩάϥϛϯά

  27. ΞεϖΫτࢦ޲ϓϩάϥϛϯά ͱ͸ʁ • Aspect Oriented Programming • ΦϒδΣΫτͷ୯ҐͰ͸͏·͘෼཭Ͱ͖ͳ͍ɺ
 ΦϒδΣΫτؒΛ·͙ͨػೳΛʮہ໘ʯͱ͍͏
 ୯Ґ͝ͱʹϞδϡʔϧԽ͢ΔϓϩάϥϛϯάύϥμΠ

    Ϝ • ΦϒδΣΫτؒΛ·͙ͨػೳͷ͜ͱΛ
 ʮCrosscutting Concern(ԣஅతؔ৺ࣄ)ʯͱݺͿ
  28. Crosscutting Concernͷྫ • Logging • Caching • Error Handling •

    Transaction
  29. ΞεϖΫτࢦ޲ϓϩάϥϛϯά Ͱ༻͍ΒΕΔ༻ޠ • Join Point • ΞϓϦέʔγϣϯ࣮ߦதͷɺ͋Δಛఆͷʮ࣌఺ʯΛࢦ͢ • Advice •

    Join PointͰ࣮ߦ͞ΕΔॲཧ • Pointcut • Join Pointͷఆٛ
  30. Spring AOP

  31. Spring AOPͷ࢖͍ํ • Join PointΛܾΊͯPointcutͱͯ͠ఆٛ͠ɺ PointcutͰߦ͏࣮ॲཧͰ͋ΔAdviceΛఆٛ͢ Δ • ಛఆͷΫϥεͷಛఆͷϝιου͕࣮ߦ͞ΕΔ લ΍ޙʹɺ࣮ߦ͢ΔॲཧΛఆٛ͢Δ

  32. Pointcutͷఆٛ @Aspect public class RepositoryPointcut { // UserRepositoryͷsaveϝιου͕࣮ߦ͞ΕΔ࣌఺(Join Point)ΛPointcutͱͯ͠ఆٛ @Pointcut("execution(*

    com.onigra.springaopsample.repository..UserRepository+.save(..))") public void save() {} }
  33. 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ϝιου࣮ߦલʹϩάΛग़ྗ͢Δ"); } }
  34. RailsͰݴ͏ͱCallback͔ͳ

  35. AOPͱCallback͸ ࣅͯඇͳΔ΋ͷ

  36. RailsͷCallback • ActiveSupport::Callbacks#run_callbacksʹ
 ϒϩοΫΛ౉͢͜ͱͰ࣮ݱ • Rubyͷݴޠͱͯ͠ͷػೳΛར༻͍ͯ͠Δ

  37. Spring AOP • όΠτίʔυม׵ • ίϯύΠϧޙͷόΠφϦΛվม͠ɺॲཧΛࠩ ͠ࠐΉ

  38. TransactionͰͷAOP • Rails͸ɺActiveRecord::Transactions.transactionʹϒϩοΫΛ౉͢ • Spring Framework͸ɺTransactionΛ͔͚͍ͨ
 ϝιουʹର͠ɺTransactionalΞϊςʔγϣϯ(෇Ճ৘ใ)Λ෇༩ • RailsͷΑ͏ʹɺϓϩάϥϜͷதͰ੍ޚΛߦ͏ํ๏ʹର͠ɺʮಛఆ ͷϝιουͷ࣮ߦ࣌ʹτϥϯβΫγϣϯΛ͔͚Δʯͱ͍͏


    એݴΛߦ͏͜ͱΛɺʮએݴతτϥϯβΫγϣϯʯͱݺͿ • ※ϓϩάϥϛϯάʹΑΔτϥϯβΫγϣϯ؅ཧ΋Ͱ͖Δ
  39. MVCͱ ϨΠϠʔυΞʔΩςΫνϟ

  40. ΞϓϦέʔγϣϯͷ ϨΠϠʔԽͷ࿩ͱ MVC(MVC2, MVC Model2)ͷ࿩͸ શ͘ผͷ࿩ʂʂʂ

  41. Railsͷ੹຿෼ׂͷ࿩͸ɺ ΞϓϦέʔγϣϯͷϨΠϠʔԽͷ࿩ͱɺ MVCͷ࿩͕ࠞͬͯ͟͠·͍ͬͯΔ ʢ͜ͱ͕ଟ͍ؾ͕͢Δʣ

  42. ͞Βʹͦ͜ʹDDDʹ͓͚ΔαʔϏεͱ PofEAAͷαʔϏεͷ࿩͕ࠞͬͯ͟ େมͳ͜ͱʹ…

  43. ΞϓϦέʔγϣϯઃܭʹ͓͚Δ ϨΠϠͷ֓೦ͱ͸ • ݩ͸Client-Server Modelͷߟ͑ํ • Webʹ΋ߟ͑ํ͕Ԡ༻͞Εɺൃల͖ͯͨ͠ • ͔͠͠ɺΞʔΩςΫνϟ΍จݙʹΑ༷ͬͯʑ ͳϨΠϠʔͷ໊শ͕ଘࡏ͢ΔͷͰ஫ҙ͕ඞཁ

  44. ΫϥΠΞϯταʔόϞσϧʹ͓ ͚Δ3૚ΞʔΩςΫνϟ • ϓϨθϯςʔγϣϯ: UI • ΞϓϦέʔγϣϯ: ϏδωεϩδοΫ • σʔλ:

    σʔλΞΫηε
  45. PofEAAʹ͓͚Δ3૚ΞʔΩςΫ νϟ • ϓϨθϯςʔγϣϯ: UI • υϝΠϯ: ϏδωεϩδοΫ • σʔλιʔε:

    σʔλΞΫηε
  46. ͜ͷجຊͱͳΔ3૚Ͱ ߟ͑ͯݟΔ

  47. Spring Framework • ϓϨθϯςʔγϣϯ • View(Template Engine(ThymeLeaf౳)) • Controller(Spring MVC)

    • υϝΠϯ • Service(Spring Core) • Repository(Spring Core) • σʔλιʔε • Template(Spring Data) • ͦΕͧΕͷ૚͔ΒModelΛѻ͏
  48. Spring Framework https://yo1000.gitbooks.io/self-study-spring/content/chapter1.html

  49. Ruby on Rails • ϓϨθϯςʔγϣϯ • View(ActionView, View Template Engine(ERB౳))

    • Controller(ActionController) • υϝΠϯ • Model(ActiveRecord, ActiveModel) • σʔλιʔε • Model(ActiveRecord)
  50. ༷ʑͳϨΠϠʔԽͷύλʔϯ͕ ଘࡏ͢Δ • ΞϓϦέʔγϣϯʹΑͬͯɺద੾ͳϨΠϠԽ ͸ҟͳΔ • จ຺΍ΞʔΩςΫνϟʹΑͬͯɺ໊শ͕ඃͬ ͨΓҟͳͬͨΓ૿͑ͨΓ͢Δͷʹ஫ҙ • ϨΠϠͷҙຯΛཧղ͠ɺద੾ͳϨΠϠΛબ୒

    ͠Α͏
  51. ·ͱΊ

  52. 2ͭͷϑϨʔϜϫʔΫΛ
 ൺֱͯ͠ • Rails͸ϢʔεέʔεΛߜͬͯɺׂΓ੾ͬͨઃܭʹ͍ͯ͠Δ • ͦͷ෼ɺϚον͠ͳ͍Ϗδωεཁ݅ʹΑͬͯɺਏΈ͸౰વग़Δ • Spring Framework͸ɺ͸͡Ί͔Β޿͍ϢʔεέʔεʹରԠ͢Δ ͜ͱΛ໨ࢦ͍ͯ͠Δ

    • ෳࡶͳϏδωεཁ݅΍ɺ௕ظతͳ֦ுͷՄೳੑΛߟ͑Δͱɺ Spring Framework͸༏Εͨબ୒ࢶ • ҰํͰɺ͋Β͔͡Ίཧղ͓ͯ͘͠΂͖จ຺ɺ஌͕ࣝଟ͍
  53. 2ͭͷϑϨʔϜϫʔΫΛ
 ൺֱͯ͠ • ઃܭͷ෯Λ٘ਜ਼ʹͭͭ͠΋ɺ֮͑Δ͜ͱΛݮΒͯ͠։ ൃ଎౓Λ্͛Δ͜ͱ͕Ͱ͖ΔActiveRecord͸΍͸Γศ ར • ੹຿΍໊শɺจ຺Λཧղ্ͨ͠ͰΞʔΩςΫνϟύλʔ ϯΛ࢖͍͜ͳ͢ͷ͸େม͔ͩΒɺେ୾ʹMVCͱ͍͏෼ ྨΛͨ͠ͷͰ͸ʁ

    • ͦ͏ߟ͑ΔͱDHHϚδͰ͍͢͝…
  54. ฏຌͳ݁࿦Ͱ͕͢ • ͲͪΒͷํ͕༏Ε͍ͯΔͱ͍͏࿩Ͱ͸ͳ͘ɺ ϑϨʔϜϫʔΫ΍ΞʔΩςΫνϟͷࢥ૝ͱ
 ϢʔεέʔεΛཧղ͠ɺద੾ͳٕज़બఆΛͯ͠ ͍͖·͠ΐ͏

  55. ҟͳΔݴޠͷϑϨʔϜϫʔΫ΍ ઃܭࢥ૝ʹ৮ΕΔ͜ͱ͸ɺ ઃܭͷ෯Λ޿͛Δ͜ͱʹͭͳ͕Δ