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

DDDの始め方

 DDDの始め方

DDDを始めるにあたって実施したことを実例を交えて紹介しています。
内容
- DDDとは何か
- なぜDDDをやるのか
- 実際にどうやって導入したか

33977264d18863fff5608189b475f1aa?s=128

Yamaki Hiroshi

July 19, 2017
Tweet

Transcript

  1. DDDͷ࢝Ίํ

  2. ࣗݾ঺հ • ീרߛ࢜(ϠϚΩ ώϩγ) • (ג)ϗϫΠτϓϥεͷΤϯδχΞ • ܦྺ͸ۀ຿ܥSE→webΤϯδχΞ • DDD΍ΦϒδΣΫτࢦ޲ͳͲͷઃܭͷ࿩͕޷͖

  3. ΞδΣϯμ DDDͬͯʁ DDDΛ΍Δಈػ ͔͜͜Β࢝ΊΔDDD

  4. DDDͬͯʁ

  5. υϝΠϯۦಈઃܭ (Domain-Driven Design) ΤϦοΫɾΤϰΝϯε͕ఏএͨ͠։ൃํ๏

  6. DDD͕໨ࢦ͢΋ͷ

  7. –Eric Evans ΤϦοΫɾΤϰΝϯεͷυϝΠϯۦಈઃܭ ʮ·͕͖͑ʯ “໺৺తͳ໨ඪΛୡ੒ͯ͠ɺاۀͷܧଓత ͳཁٻΛຬͨ͢΂͘ਐԽ͠ଓ͚Δෳࡶͳ ιϑτ΢ΣΞΛ࡞Γग़͢͜ͱ”

  8. ͬ͘͟Γݴ͏ͱɺ ෳࡶͳγεςϜΛϨΨγʔʹͤͣɺ ਐԽͤ͞ଓ͚ΔͨΊͷ։ൃํ๏ (໨ࢦ͞ͳ͍΋ͷ) • ୯७ͳ΋ͷΛૉૣ͘࡞Δ • Ұ࣌తͳ࢖͍ࣺͯεΫϦϓτͷ࡞੒ • ࡞ͬͨΒϝϯς͠ͳ͍ιϑτ΢ΣΞ

  9. DDDͷಛ௃

  10. –Martin Fowler ΤϦοΫɾΤϰΝϯεͷυϝΠϯۦಈઃܭ ʮংจʯ “ιϑτ΢ΣΞ։ൃΛෳࡶʹ͢ΔཁҼ͸਺ ଟ͘ଘࡏ͢Δɻ͔͠͠ɺ͜ͷෳࡶ͞ͷ֩ ৺ʹ͋Δͷ͸ɺ໰୊υϝΠϯࣗମ͕ຊ࣭ తʹෳࡶͰ͋Δͱ͍͏ࣄ࣮ͩɻ”

  11. γεςϜͷෳࡶ͞ͷཁҼ • γεςϜԽ͢Δۀ຿(=υϝΠϯ)͕ෳࡶ • ͜ͷυϝΠϯΛϞσϧԽ͢Δ͜ͱͰγες ϜΛίϯτϩʔϧͰ͖Δ΋ͷʹ͢Δ ٕज़ྖҬͰ͸ͳ࣮͘ࡍͷۀ຿ʹண໨ͯ͠ਐΊ ͍ͯ͘ͷ͕ಛ௃

  12. Ͳ͏΍࣮ͬͯݱ͢Δ͔ 1. ࣮ࡍͷۀ຿ΛϞσϦϯά(υϝΠϯϞσϧ) 2. υϝΠϯϞσϧΛίʔυͰදݱ 3. ಈ͘ίʔυΛݟͤɺϑΟʔυόοΫΛड͚Δ ͜ΕΛճ͠ͳ͕ΒυϝΠϯϞσϧΛચ࿅ͤ͞Δ

  13. DDDΛ΍Δಈػ

  14. ಈػᶃɿ मਖ਼ɾվमͷӨڹൣғΛௐ΂ Δ͜ͱ͕େม

  15. DDDͰ͸ ۀ຿͕ϞσϦϯάͷத৺ ۀ຿ͷมߋ͕γεςϜ(Ϟσϧ)ͷม ߋʹͳΔ ద੾ͳϞσϦϯά͕͞ΕΕ͹ɺ ۀ຿ʹؔ܎ͷͳ͍γεςϜʹӨڹ͸Ͱͳ͍

  16. ಈػᶄɿ (ৄࡉ͗͢Δ)υΩϡϝϯτΛ ॻ͖ͨ͘ͳ͍ (ϝϯς͞Εͳ͍ΑͶʁ) (ίʔυಡΉΑͶʁ)

  17. DDDͰ͸ ϞσϧΛίʔυͰදݱ͢Δ υΩϡϝϯτͰ͸ͳ͍ ςετίʔυΛؚΊͯɺ ίʔυ͕ୈҰڃͷυΩϡϝϯτ

  18. ಈػᶅɿ ϨΨγʔʹͳͬͯ͠·ͬͨγ εςϜΛͲ͏ʹ͔͍ͨ͠

  19. DDDͷ໨త͸ γεςϜΛϨΨγʔʹͤͣɺ ܧଓతʹਐԽͤ͞Δ͜ͱ

  20. DDDͷ·ͱΊ • γεςϜ͕ෳࡶʹͳΔͷ͸ѻ͏ۀ຿͕ෳࡶ ͔ͩΒ • ۀ຿ͷϞσϦϯά(υϝΠϯϞσϧ)Λઃܭͷ த৺ʹ͢Δ • ϞσϦϯάˠίʔσΟϯάˠϑΟʔυόοΫ ͷαΠΫϧͰυϝΠϯϞσϧΛચ࿅ͤ͞Δ

    • γεςϜΛϨΨγʔʹͤͣɺܧଓతʹਐԽ͞ ͤΔ
  21. ͔͜͜Β࢝ΊΔDDD ϗϫΠτϓϥεͰ࣮ફͨ͜͠ͱͷ঺հ

  22. 1. ΞʔΩςΫνϟͷಋೖ ·ͣ͸DDDΒ͘͠ॻ͚Δ؀ڥΛ੔උ

  23. ಋೖͨ͠ΞʔΩςΫνϟ

  24. 3૚ʴυϝΠϯ૚ ίϯτϩʔϥʔ ΠϯϑϥετϥΫνϟ ΞϓϦέʔγϣϯ υϝΠϯ

  25. ֤૚ͷઆ໌

  26. υϝΠϯ૚ ίϯτϩʔϥʔ ΠϯϑϥετϥΫνϟ ΞϓϦέʔγϣϯ υϝΠϯ

  27. υϝΠϯ૚ • υϝΠϯϞσϧ͕ଉͮ͘৔ॴ • ϏδωεϩδοΫΛॻ͘ • DB΍UIͳͲͷٕज़ελοΫͷૢ࡞͸͜͜ʹ ͸ॻ͔ͳ͍

  28. ίϯτϩʔϥʔ૚ ίϯτϩʔϥʔ ΠϯϑϥετϥΫνϟ ΞϓϦέʔγϣϯ υϝΠϯ

  29. ίϯτϩʔϥʔ૚ • UI΍όονίϚϯυͷड͚ޱ • ϦΫΤετ΍Ҿ਺ͷݕূΛߦ͍ΞϓϦέʔ γϣϯ૚ͷαʔϏεΛݺͼग़͢ίʔυΛॻ ͘

  30. ΞϓϦέʔγϣϯ૚ ίϯτϩʔϥʔ ΠϯϑϥετϥΫνϟ ΞϓϦέʔγϣϯ υϝΠϯ

  31. ΞϓϦέʔγϣϯ૚ • ֎෦ެ։ΠϯλʔϑΣʔε • Πϯϑϥ૚ͱυϝΠϯ૚ͷڮ౉͠ • ΞϓϦέʔγϣϯΛ࣮ߦ͢Δखଓ͖Λॻ͘

  32. ΠϯϑϥετϥΫνϟ૚ ίϯτϩʔϥʔ ΠϯϑϥετϥΫνϟ ΞϓϦέʔγϣϯ υϝΠϯ

  33. ΠϯϑϥετϥΫνϟ૚ • DBɺϑΝΠϧɺΩϟογϡͳͲΠϯϑϥʹ ΞΫηε͢ΔίʔυΛॻ͘

  34. σΟϨΫτϦߏ੒ Xxxx ├── App │ └── XxxxService.php ├── Domain │

    └── Xxxx.php ├── Infra │ └── XxxxRepository.php └── UI └── XxxxController.php
  35. ·ͣ͸͡Ίʹ໨ࢦͨ͜͠ͱ

  36. ৽͍͠ΞʔΩςΫνϟͰ౰ͨ Γલͷ͜ͱΛ࣮ݱ͢Δ • ը໘ೖྗ͔ΒDBొ࿥ • DB͔ΒσʔλΛऔಘͯ͠ɺը໘දࣔ

  37. ࣍ʹ໨ࢦ͍ͯ͠Δ͜ͱ

  38. ίϯτϩʔϥʔͳͲͰॻ͔Ε ͨ1ຕؠͷίʔυΛ֤ϨΠ Ϡʔ΁Ҡಈ͢Δ

  39. ͨͩ͠ʂ ࠷ѱίϯτϩʔϥʔ૚ʹॻ͔Εͨίʔυ͸Ξ ϓϦέʔγϣϯ૚ʹ͔͚͹ಈ͖·͢ɻ ͱ͍͏৺ཧత҆શωοτ΋Ҋ಺͢Δɻ

  40. ·ͱΊ • ΞʔΩςΫνϟΛಋೖͯ͠·ͣ͸DDDͰ ॻ͚Δ৔ॴ͕֬อ͞Εͨ • ࠓ·ͰϢχοτςετ͕ॻ͚ͳ͔ͬͨίʔ υͰςετ͕ॻ͚ΔΑ͏ʹͳͬͨ • (ͦͷ෼࣌ؒ΋͔͔ΔΑ͏ʹͳͬͨ)

  41. 2. υϝΠϯϞσϧΛҭͯΔ ͍Α͍ΑDDDͰ࣮ࡍʹίʔυ͕ॻ͚Δ

  42. υϝΠϯϞσϧΛҭͯΔࡍʹ ग़͖ͯͨ໰୊

  43. ໰୊ᶃɿ Ͳ͏͍͏ج४ͰίʔυΛ·ͱ Ί͍ͯͬͨΒྑ͍ͷ͔ʁ

  44. ࣗ෼ͨͪͷαʔϏε͕ͲΜͳ ۀ຿Ͱ੒Γཱ͍ͬͯΔ͔੔ཧ ͢Δ →αϒυϝΠϯΛચ͍ग़͢

  45. αϒυϝΠϯ • ֤ۀ຿୯ҐͷྖҬ • ҙࢥܾఆऀɺओʹؔΘΔਓୡɺൃੜ͢ΔΠ ϕϯτͷछྨͳͲͰઢΛ۠੾Δ

  46. ΍͍ͬͯΔ༷ࢠ

  47. ໰୊ᶄɿ ࣗ෼ͨͪͷγεςϜ͸DDDత ʹ͸Ͳ͏ͳͷʁ

  48. ࣗ෼ͨͪͷαʔϏε͕ͲΜͳ γεςϜͰ੒Γཱ͍ͬͯΔ͔ ੔ཧ͢Δ • ڥք͚ͮΒΕͨίϯςΩετΛચ͍ग़͢ • αϒυϝΠϯͱϚοϐϯά͢Δ

  49. ڥք͚ͮΒΕͨίϯςΩετ • αϒγεςϜɺ֎෦γεςϜͳͲͷൺֱత ໨ʹݟ͑ΔγεςϜͷڥ໨

  50. Կ͕෼͔Δ͔ʁ • αϒγεςϜͱ֎෦γεςϜΛআ͘ڥք͚ͮ ΒΕͨίϯςΩετ͸1:1͕ཧ૝͕ͩɺݱঢ় ͕Ͳͷ͘Β͍཭Ε͍ͯΔ͔෼ੳͰ͖Δ • αϒυϝΠϯ୯ҐͰτοϓϨϕϧͷ໊લۭ ͕ؒखʹೖΔ

  51. Xxxx ├── App │ └── XxxxService.php ├── Domain │ └──

    Xxxx.php ├── Infra │ └── XxxxRepository.php └── UI └── XxxxController.php ˡ͜Ε͕ܾ·Δ Կ͕෼͔Δ͔ʁ
  52. ·ͱΊ • ࠓޙυϝΠϯΛҭ͍ͯͯ͘ํ޲ੑ͕ݟ͑ ͨ • ࣗ෼ͨͪͷγεςϜ๊͕͑ΔυϝΠϯͷ ཧղ͕ਂ·ͬͨ • ݱ࣮Λ஌ͬͨ

  53. ࠓޙ΍͍ͬͯ͘͜ͱ • ֤ਓͰ࢝Ί͍࣮ͯͨ૷ͷ౷߹ • ࣮૷ϨϕϧͰͷඪ४Խ • ʮڊେͳటஂࢠʯͱͷઓ͍͸ଓ͘

  54. ͓ΘΓ ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠

  55. ࣮ࡍͷίʔυͷ঺հ

  56. class Period { public function shorten(DueDate $renewal_day) { if (!$this->canShorten($renewal_day))

    { throw new \OutOfRangeException("࣍ճߋ৽೔͕༗ޮͳൣғʹ͋Γ·ͤΜɻ"); } return new Period($this->startDay, $renewal_day); } public function canShorten(DueDate $renewal_day) { $candidate = $this->candidateShortenPeriod(); return $candidate->includes($renewal_day); } public function candidateShortenPeriod() { // ݄࣍ͷ1೔ʙݱࡏͷ࣍ճߋ৽೔·Ͱ͕୹ॖͷީิ return new static( new DueDate(new \DateTimeImmutable('first day of next month')), $this->renewalDay() ); } public function includes(DueDate $renewal_day) { return ($renewal_day == $this->startDay || $renewal_day->after($this- >startDay)) && ($renewal_day == $this->renewal_day || $renewal_day->before($this- >renewal_day)); } }