Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DDD実践(ベスト)プラクティス ~ドメインイベントとマイクロサービスと組織の関係~

DDD実践(ベスト)プラクティス ~ドメインイベントとマイクロサービスと組織の関係~

かとじゅん

October 25, 2016
Tweet

More Decks by かとじゅん

Other Decks in Programming

Transcript

  1. DDD࣮ફϕετϓϥΫςΟε 2016/3/26 © ChatWork All rights reserved. ࣗݾ঺հ w!KJLP w$IBU8PSLͰςοΫϦʔυΛ΍͍ͬͯ·͢ɻ

    wաڈ͸4FBTBS+BWBɺࠓ͸4DBMB"LLB͕ओઓ৔ɻ w࠷ۙ͸$234 &4 "LLBͳͲΛ࢖ͬͯ࢓ࣄΛͯ͠ ͍·͢ɻ w%%%Ͱ໎ࢠʹͳͬͯͨΒ੠͔͚͍ͯͩ͘͞ɻΞυ όΠε͠·͢ɻ
  2. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. ΞδΣϯμ wλΠτϧઌߦͰߟ͑ͨˠॻ͍͍ͯΔ͏ͪʹϕ ετͳϓϥΫςΟε͡Όͳͯ͘΋͍͍ͱͳͬ

    ͨͷͰɺϕετ͔Ͳ͏͔͸͓͖ͯ͞ͱ͍͏લ ఏͰʜɻ w $234ͱ͸ w $234 &WFOU4PVSDJOHͱ͸ w υϝΠϯΠϕϯτͱϚΠΫϩαʔϏε w ϚΠΫϩαʔϏεͱίϯ΢ΣΠͷ๏ଇ w ίϯ΢ΣΠͷ๏ଇͱ%%%ͷؔ܎ੑ
  3. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 6 $234ͱ͸ w

    $PNNBOEBOE2VFSZ3FTQPOTJCJMJUZ4FHSFHBUJPO wίϚϯυɾΫΤϦ੹຿෼཭ w ೥(SFH:PVOHࢯ͕ߟҊͨ͠ύλʔϯɻ w ೥ʹ#FSUSBOE.FZFSࢯ͕ߟҊͨ͠ίϚϯυΫΤϦ෼ ཭ݪଇ $PNNBOE2VFSZ4FQBSBUJPO$24 ΛΞʔΩς Ϋνϟʹద༻ͨ͠΋ͷ͕$234ɻ w ʮ͋ΒΏΔϝιου͸ɺΞΫγϣϯΛ࣮ߦ͢ΔίϚϯ υ͔ɺݺͼग़͠ݩʹσʔλΛฦ͢ΫΤϦ͔ͷ͍ͣΕ͔ Ͱ͋ͬͯɺ྆ํΛߦͬͯ͸ͳΒͳ͍ɻ͜Ε͸ɺ࣭໰Λ ͢Δ͜ͱͰճ౴ΛมԽͤͯ͞͸ͳΒͳ͍ͱ͍͏͜ͱ ͩɻʯ
  4. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 7 ετϨʔδΛ෼͚ͳ͍$234ΞʔΩςΫνϟ w

    $24ʹج͍ͮͨ࠷΋୯७ͳߏ੒͸ɺυϝΠϯ૚ΛϥΠτͱϦʔυͷܥ Λ෼͚Δɻ Client Write Stack Read Stack Database Domain Layer Application Layer Infrastructure Layer Domain Layer Application Layer Infrastructure Layer
  5. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 8 ετϨʔδ΋෼͚Δ$234ΞʔΩςΫνϟ w

    ϥΠτͱϦʔυܥΛ෼ੳ͍ͯ͘͠ͱɺϦʔυଆ͸ΫΤϦཁ݅ʹରԠ͢ Δͷ͕໨తͳͷͰυϝΠϯϞσϧ͕ෆཁʹͳΔɻ Client Write Stack Read Stack Database Domain Layer Application Layer DAO + DTO Read Records Infrastructure Layer Write Records ReadModel ܗࣜม׵Λߦ͏
  6. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 10 υϝΠϯΠϕϯτ͕ओ໾ʹͳΔ&WFOU4PVSDJOH w

    (SFH:PVOHࢯߟҊ w &WFOU4PVSDJOH ҎԼ&4 ͱ͸ɺσʔλͰ͸ͳ͘Կ͔͠Βͷग़དྷࣄʹυϝ ΠϯΠϕϯτΛϞσϦϯάͷओ໾ͱ͢Δ͜ͱɻ w υϝΠϯϞσϧΛσʔλͱͯ֨͠ೲ͢ΔͷͰ͸ͳ͘ɺൃੜ͢ΔυϝΠϯΠ ϕϯτΛ͢΂ͯӬଓԽ͢Δɻ جຊతʹ௥Ճ͔͠͠ͳ͍  w &4͸$234Λܶతʹվྑ͢Δ ҎԼ$234 &4 ɻ CartItemAdded { id, cartId, itemId, itemCount } CartItemCountUpdated { id, cartId, itemId, itemCount } CartCreated { id, cartId, userId } CartItemRemoved { id, cartId, itemId } …
  7. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 11 $234 &4ΞʔΩςΫνϟ

     w γεςϜ͸ू໿Ͱൃੜͨ͠υϝΠϯΠϕϯτͰۦಈ͠Πϕϯτ͸δϟʔφϧ ͱͯ͠ӬଓԽ͞ΕΔɻΠϕϯτ͔ΒͷϦϓϨΠ͸4OBQTIPU+PVSOBMɻ Client Write Stack Read Stack Data Store DAO + DTO Read Records Aggregate Application Layer Infrastructure Layer Event Store Journals Snapshots Πϕϯτͷอଘઌ CartCreated CartItemAdded … ͢΂ͯͷΠϕϯτΛ ӬଓԽ Snapshot+journalsͰ ϦϓϨΠ
  8. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 12 $234 &4ΞʔΩςΫνϟ

     w ίϚϯυʹΑͬͯੜͨ͡ू໿ͷঢ়ଶมԽΛΠϕϯτͱ͠ ͯ௨஌͢Δɻ Domain Layer Aggregate Aggregate Aggregate Command Command Event Command/Event Handler Event Event Event Command Command/Event Handler Command/Event Handler Event Event Event
  9. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 13 υϝΠϯΠϕϯτͱϚΠΫϩαʔϏε w

    #$ؒͷ࿈ܞʹ΋υϝΠϯΠϕϯτ͕ར༻ՄೳʹͳΔɻϚΠΫϩαʔϏεΞʔΩ ςΫνϟͱ૬ੑ͕Α͍ɻ w z֎෦ͷଞͷڥք͚ͮΒΕͨίϯςΩετʹ͸໌ࣔతͳΠϯλʔϑΣΠε͕͋ ΓɺͦͷΠϯλʔϑΣΠε͕ଞͷίϯςΩετͱڞ༗͢ΔϞσϧΛܾఆ͠· ͢ɻz ϚΠΫϩαʔϏεΞʔΩςΫνϟΑΓ  w Πϕϯτʹ͸#$಺෦͚ͩͰൃੜɾফඅ͞ΕΔӅΕΠϕϯτͱ֎෦ͷ#$͕ؔ৺ Λ࣋ͭڞ༗Πϕϯτ͕ଘࡏ͢Δɻ BC (2) Application Domain Layer Aggregate BC (1) Application Domain Layer Aggregate BC (3) Application Domain Layer Aggregate Event Event Event
  10. DDD࣮ફϕετϓϥΫςΟε ग़ՙ؅ཧνʔϜ ࡏݿ؅ཧνʔϜ 2016/10/24 © ChatWork All rights reserved. 15

    ϚΠΫϩαʔϏεͱίϯ΢ΣΠͷ๏ଇ  w lγεςϜΛઃܭ͢Δ͋ΒΏΔ૊৫͸ɺඞͣͦͷ ૊৫ͷίϛϡχέʔγϣϯߏ଄ʹ฿ͬͨߏ଄Λ ࣋ͭઃܭΛੜΈग़͢ɻzŠ.FMWJO$POXBZ ϝ ϧϰΟϯɾίϯ΢ΣΠ  w "NB[PO w νʔϜʹαʔϏε w ։ൃɾӡ༻ͳͲͷϥΠϑαΠΫϧͷҰ੾ͷ ੹೚Λ࣋ͭ w ຕͷϐβνʔϜ w ຕͷϐβͰ଍Γͳ͍΄ͲͷνʔϜ͸࡞Β ͳ͍ w /FUqJYͰ΋࠷ॳ͔Βখن໛ͳνʔϜͰߏ੒͠ɺ ΞʔΩςΫνϟͷͨΊʹ૊৫ߏ଄Λઃܭͨ͠ɻ ࡏݿ؅ཧγεςϜ ग़ՙ؅ཧγεςϜ ൢച؅ཧνʔϜ ൢച؅ཧγεςϜ ࢓ೖ؅ཧνʔϜ ࢓ೖ؅ཧγεςϜ
  11. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 16 ϚΠΫϩαʔϏεͱίϯ΢ΣΠͷ๏ଇ 

    w ୯ҰγεςϜ୯ҰνʔϜ w γεςϜ಺ͷดཻͨ͡౓ͷࡉ͔͍ίϛϡχ έʔγϣϯಛੑΛ͍࣋ͬͯΔɻαʔϏε಺ͷ มߋ΍ϦϑΝΫλϦϯά͸ޮ཰తɻ w ୯ҰγεςϜෳ਺νʔϜ w ཻ౓ͷࡉ͔͍ดͨ͡ωοτϫʔΫ͸ہॴԽ͞ ΕΔՄೳੑ͕͋Δ ஍ཧతڥք͕͋Δ৔߹ಛʹ ɻ νʔϜؒΛލ͕Δ৔߹͸ͦͷಛੑΛܽ͘৔߹ ͕͋Δɻมߋ΍ϦϑΝΫλϦϯάͷίετ΋ ૬ରతʹߴ͘ͳΔɻେن໛γεςϜͷอक͕ ೉͘͠ͳΔݪҼ w ڞ༗αʔϏεʹ޲͔͏ཧ༝ w γεςϜͷ෼ׂʹߴ͍ίετΛ෷͏έʔεɻ w ෳ਺ͷαʔϏεʹލ͕Δมߋ͕σϦόϦʔί ετΛ͋͛ͯ͠·͏έʔεɻ νʔϜ γεςϜ νʔϜ νʔϜ γεςϜ φϩʔόϯυ ϒϩʔυόϯυ
  12. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 17 ίϯ΢ΣΠͷ๏ଇͱ%%%ͷؔ܎ੑ w

    ϚΠΫϩαʔϏεͱ͸ɺٕज़υϝΠϯͰ͸ͳ͘ɺϏδωευϝ Πϯʹ฿ͬͯϞσϧԽ͞ΕͨαʔϏεɻ w ϞσϦϯά࡞ۀʹݴޠೳྗΛ׆༻͢Δ͜ͱͷҙຯ w ਤΛඳ͘ʹ͸ࢹ֮తɾۭؒతͳࢥߟΛۦ࢖͢Δɻ෼ੳ͢Δࡍ ʹίʔυͷײ֮Λ࢖͏ͷͱಉ༷ɻݴޠΛ࢖͏͜ͱ͸ͦΕΒͱ มΘΒͳ͍ɻ w ໾ʹཱͭϞσϧͱઃܭΛݟ͚ͭΔʹ͸͜ͷͭ͢΂͕ͯඞཁ ʹͳΔ͕ݴޠʹΑΔ࣮ݧ͸ݟա͝͞ΕΔ͜ͱ͕࠷΋ଟ͍ɻ w ڥք͚ͮΒΕͨίϯςΩετʹϞσϧͷೄுΓ w ͭͷνʔϜʹͭͷݴޠɻͭͷڥքʹෳ਺ͷϞσϧ͕ଘࡏ ͢Δ৔߹͕͋Δ͕ɺผʑͷϞσϧ͕ࠞࡏ͢ΔͱόάͷԹচ΍ ৴པੑͷ௿Լ΍ཧղ͕ͨ͘͠ͳΔɻνʔϜͷίϛϡχέʔ γϣϯ΋ࠞཚΛདྷ͢ɻ w Ϟσϧ͕ద༻͞ΕΔڥքΛ໌ࣔతʹఆٛ͢Δ͜ͱɻͦΕ͸ɺ νʔϜฤ੒ɺΞϓϦέʔγϣϯಛ༗ͷ༻్ɺίʔυϕʔε΍ εΩʔϚͳͲͷ෺ཧతͳදݱ͔Βઃఆ͢Δɻͦͷڥք಺Ͱ ͸ɺϞσϧΛݫີʹҰ؏ੑͷ͋Δ΋ͷʹอͭ͜ͱɻ ਤ ίʔυ ݴޠ ίϯςΩετ Ϟσϧ ίϯςΩετ Ϟσϧ νʔϜ νʔϜ
  13. DDD࣮ફϕετϓϥΫςΟε 2016/10/24 © ChatWork All rights reserved. 18 ·ͱΊ w

    $234 w ઃܭ͕γϯϓϧʹͳΔɻϦʔυܥͷϢʔεέʔεΛ૝ఆ ͨ͠υϝΠϯϞσϦϯά͔Βղ์͞ΕΔɻ w $234 &4 w $234Λ͞Βʹվྑ͢ΔɻυϝΠϯϞσϧ͸σʔλͱ͍ ͏ΑΓग़དྷࣄΠϕϯτͷू߹Ͱ͋Δͱ͍͏ࢹ఺͕ಘΒ ΕΔɻ͢΂͕ͯΠϕϯτͰۦಈ͢Δੈքɻ w ͱ͸͍͑ɺΞʔΩςΫνϟ͸૊৫ͱͷؔ܎ੑΛແࢹͰ͖ ͳ͍ w ΞʔΩςΫνϟͷ෼཭ɾ౷߹ઓུ͸ɺ૊৫΍ίϛϡχ έʔγϣϯʹӨڹΛ૬ޓʹ༩͑߹͏ɻ