$30 off During Our Annual Pro Sale. View Details »

DDD導入して感じた課題(実装コスト) その解決のために行ったこと

DDD導入して感じた課題(実装コスト) その解決のために行ったこと

More Decks by 株式会社ビットキー / Bitkey Inc.

Other Decks in Technology

Transcript

  1. DDDಋೖͯ͠ײͨ͡՝୊(࣮૷ίετ) ͦͷղܾͷͨΊʹߦͬͨ͜ͱ

  2. ࠤ౻୓ਓ 4BUP5BLVUP    େֶ ݐஙֶઐ߈ ଔۀޙɺ גࣜձࣾϫʔΫεΞϓϦέʔγϣϯζʹೖࣾ ձܭγεςϜͷιϑτ΢ΣΞ։ൃΛ୲౰

    ಛʹࡒ຿ձܭͷ࢓༁ؔ࿈ ϏοτΩʔ΁ࢀը &$αΠτͷ։ൃอकɺࣾ಺γεςϜͷ։ൃ 5B,V5Zͷ։ൃ ࠓͷ)PNFͷલ਎ͱͳΔ3FTJEFODFνʔϜʹ഑ଐ CJUMPDLΛѻ͏؅ཧը໘΍όοΫΤϯυɺBQQͷ։ൃ ʹैࣄ  )PNFϓϩμΫτ ෳࡶͳࣄ৅ΛಡΈղ͍ͯߏ଄Խ͠ɺந৅Խ൚༻Խ Ͱ͖ΔΑ͏ʹઃܭ͠ɺ௿ίετͰଟ͘ͷՁ஋Λͩͤ Δ։ൃΛ͢Δ͜ͱΛ޷Ή
  3. 1. ͸͡Ίʹ ࿩͢಺༰ DDDΛಋೖͯ͠Έͯײͨ͡՝୊(࣮૷ίετ) ΁ͷରԠํ๏ͱߟ͑ͨ͜ͱ  

  4. ͸͡Ίʹ  

  5.   ͸͡Ίʹ

  6.   ͸͡Ίʹ

  7. ͸͡Ίʹ ಛ௃ ର৅ྖҬ͕ͻΖ͍ ྖҬΛΧόʔ(ͭͳ͛Δ)͜ͱͰՁ஋͕ग़Δ ݶΒΕͨϦιʔεͰ೗Կʹޮ཰తͳ։ൃ͢Δ͔ 1ͭͷ։ൃͰଟ͘ͷՁ஋͕ग़ͤΔ։ൃΛॏࢹ  

  8. ͸͡Ίʹ ଟ͘ͷػೳΛര଎Ͱ։ൃͨ݁͠Ռ...  

  9.  ՝୊ - ଞػೳͰෆ੔߹ͱͳΔσʔλΛ࡞Δ࣮૷͕Մೳ - Өڹൣғͷ೺Ѳࠔ೉Ͱɺಛఆσʔλར༻ՕॴνΣοΫ͕ඞཁ & ͭΒ͍ - ௐࠪ࣌ͳͲσʔλ͔Βݱঢ়ͷεςʔλε೺Ѳ͕ࠔ೉

    (σʔλભҠ) - σʔλमਖ਼࣌ʹඞཁͳՕॴͷચ͍ग़͕͠ࠔ೉ (ػೳ × σʔλͷؔ࿈) - validation౳ͷϩδοΫ͕ࢄཚͯ͠ػೳमਖ਼͕େม… - ొ࿥஋ͷೝࣝͷ࢓ํ͕֤ػೳࠩҟ͕͋Γػೳमਖ਼࣌ʹͭΒ͍… - Ͳ͜ʹͲͷΑ͏ͳ࣮૷͞Ε͍ͯΔ͔ೝ͕ࣝͣΕΔ͜ͱ͕͋Δ - service૚ˠservice૚ˠ...→service૚ˠ update & commit  ͸͡Ίʹ
  10. 1. ݎ࿚ੑ ར༻͢Δଆͷ࣮૷ͷ࢓ํʹΑΒͣɺϧʔϧ͕ݫक͞ΕΔ 1. ஌ࣝͷू໿ ར༻ՕॴͷιʔεΛղಡ͢Δ͜ͱͳ͘ɺڍಈͷ೺Ѳ͕Մೳ   ͸͡Ίʹ ՝୊ղܾͷϙΠϯτ

  11. 1. ݎ࿚ੑ ར༻͢Δଆͷ࣮૷ͷ࢓ํʹΑΒͣɺϧʔϧ͕ݫक͞ΕΔ 1. ஌ࣝͷू໿ ར༻ՕॴͷιʔεΛղಡ͢Δ͜ͱͳ͘ɺڍಈͷ೺Ѳ͕Մೳ   ͸͡Ίʹ ˠ͜ΕΛղܾ͢ΔͨΊʹ%%%ͷ࢓૊ΈΛར༻

    ˞ϘτϜΞοϓͷߟ͑ํ ՝୊ղܾͷϙΠϯτ
  12. ΍ͬͯΈͯ  

  13. ͦͷ݁Ռ ΍ͬͯΈͯ  

  14. ΍ͬͯΈͯ   ϑΝΠϧ͕૿͑ͯ ͳΜ͔࣮૷ྔ͕૿͑ͯ...ͭΒ͍

  15. ΍ͬͯΈͯ   ྫ ݐ෺ͷొ࿥ͷॲཧ 1ͭͷݐ෺ʹෳ਺ͷ෺͕݅ඥͮ͘ ݐ෺ + ෺݅ Λू໿ͱ͢Δ

  16. ΍ͬͯΈͯ   BuildingFirestoreType BuildingFirestoreCollectionPath PropertyFirestoreType PropertyFirestoreCollectionPath RegisterBuildingUsecase σʔλϕʔε(Firestore)ͷఆٛ σʔλऔಘ/ߋ৽͸΋ͪΖΜ

    όϦσʔγϣϯͷॲཧ σʔλͷՃ޻΋
  17. ΍ͬͯΈͯ   BuildingFirestoreType BuildingFirestoreCollectionPath PropertyFirestoreType PropertyFirestoreCollectionPath RegisterBuildingUsecase σʔλϕʔε(Firestore)ͷఆٛ (ͦͷ··)

  18. ΍ͬͯΈͯ   BuildingFirestoreType BuildingFirestoreCollectionPath PropertyFirestoreType PropertyFirestoreCollectionPath RegisterBuildingUsecase BuildingEntity BuildingRepository

    AddressValueObject PropertryFirstCollection PropertyEntity RentChargeValueObject PropertyStatusValueObject CheckDuplicationDomainService υϝΠϯ σʔλϕʔε(Firestore)ͷఆٛ (ͦͷ··)
  19. ΍ͬͯΈͯ   BuildingFirestoreType BuildingFirestoreCollectionPath PropertyFirestoreType PropertyFirestoreCollectionPath RegisterBuildingUsecase BuildingEntity BuildingRepository

    AddressValueObject PropertryFirstCollection PropertyEntity RentChargeValueObject PropertyStatusValueObject CheckDuplicationDomainService υϝΠϯ BuildingRepositoryFirestore BuildingFirestoreConverter PropertyFirestoreConverter σʔλϕʔε(Firestore)ͷఆٛ (ͦͷ··) ϞσϧΛIFʹ σʔλϕʔεʹ ΞΫηε͢Δॲཧ
  20. ΍ͬͯΈͯ ৽͘͠EntityΛ࡞ͬͯ...ͬͯͳΔͱ...͜ΕΒΛ࣮૷͠ͳ͚Ε͹ͳΒ ͳ͍...ɻ ͜Ε͚ͩͰɺ - ؾ࣋ͪͱͯ͠ԯ߷ʹͳΔ - ॳಈ͕஗͘ͳΔ  

    …ͷͰɺ͜ΕΛղܾ͍ͨ͠ʂ
  21. ΍ͬͯΈͯ usecaseʹهࡌ͍ͯͨ͠ॲཧΛద੾ʹ੾Γग़͍ͯ͠Δ ͱ͍͏఺͸ϝϦοτʂ͕ͩ... ϑΝΠϧ਺ͷ૿Ճ ϑΝΠϧ෼ׂʹ൐͏Interfaceͷ௥Ճ Ϋϥεͷఆٛʹ൐͏࣮૷ྔͷ૿Ճ Ϟσϧͱσʔλͷม׵ॲཧ ͸ͭΒ͍...ɻ  

  22. ΍ͬͨ͜ͱ  

  23. ͭΒ͍͜ͱ / ղܾ͍ͨ͜͠ͱ ΍ͬͨ͜ͱ  

  24. ͭΒ͍͜ͱ / ղܾ͍ͨ͜͠ͱ ΍ͬͨ͜ͱ   ػցతͳ࡞ۀ͕ଟ͍... (ଟ͘ײ͡Δ)

  25. ͭΒ͍͜ͱ / ղܾ͍ͨ͜͠ͱ ΍ͬͨ͜ͱ   ػցతͳ࡞ۀ͕ଟ͍... (ଟ͘ײ͡Δ) → ػցతͳ෦෼͸ࣗಈԽʂ

    (πʔϧ࡞ͬͯࣗಈԽͨ͠)
  26. ΍ͬͨ͜ͱ   BuildingFirestoreType BuildingFirestoreCollectionPath PropertyFirestoreType PropertyFirestoreCollectionPath RegisterBuildingUsecase BuildingEntity BuildingRepository

    AddressValueObject PropertryFirstCollection PropertyEntity RentChargeValueObject PropertyStatusValueObject CheckDuplicationDomainService BuildingRepositoryFirestore BuildingFirestoreConverter PropertyFirestoreConverter ྫ͑͹... Buildingͷ৔߹
  27. ΍ͬͨ͜ͱ  

  28. ΍ͬͨ͜ͱ   ϑΟʔϧυ͸ ಠࣗཁૉ constructor͸ػցతʹܾ·Δ ΠϯελϯεԽ͢ΔͨΊͷϝιου͸ ػցతʹܾ·Δ෦෼΋͋Δ͕ ॳظ஋౳͸ݸผ࣮૷ σʔλΛݩʹΠϯελϯεԽ͢Δ৔߹͸

    reconstructϝιουΛհ͢ΔΑ͏ʹ͓ͯ͠Γ ػցతʹܾ·Δ ଞݸผϝιου͸ݸผ࣮૷
  29. ΍ͬͨ͜ͱ   buildingRepository.ts IDΛݩʹऔಘ͢Δϝιου͸ อଘ͢Δϝιου͸ ݪଇඞཁͱͳΔͷͰ ػցతʹ࡞ͬͯ͠·͑Δ ಛఆͷϑΟʔϧυͰͷߜΓࠐΈ ͸

    ݸผ࣮૷͕ඞཁ buildingFirestoreType.ts Entityͱ࣋ͪํม͑ΔͳΒݸผఆ͕ٛඞཁ Entityͱಉ࣋ͪ͡ํ͢ΔͳΒػցతʹܾ·Δ (※EntityͷFirstCollection͸আ֎)
  30. ΍ͬͨ͜ͱ   Entityͱσʔλͷ࣋ͪํ͕ҟͳΔ৔ ߹ ʹ͸ػցతʹߦ͏͜ͱ͸೉͍͕͠ Entityͱσʔλͷ࣋ͪํ͕ಉ͡৔߹ ʹ͸ػցతʹܾΊΔ͜ͱ͕Մೳ σʔλΛݩʹEntityʹม׵͢Δॲཧ Ϟσϧಠࣗͷ࣮૷Λ͢ΔΘ͚Ͱ͸ͳ

    ͍͕൥ࡶʹͳΓ͕ͪ.... ϑΟʔϧυ๨Εͯͯɺಛఆͷ஋͕Ҿ ͖ܧ͕Εͳ͍...ͬͯ͜ͱ΋...
  31. ΍ͬͨ͜ͱ   buildingRepositoryFirestore.ts buildingͱpropertyͷσʔλͷϦϨʔγϣ ϯͷ΋ͨͤํΛػցతʹܾΊΒΕΔ࢓૊ Έͱ͓͚ͯ͠͹ػցతʹܾ·Δ هࡌ͍ͯ͠Δͷ͸propertyʹbuilding+Idͷ จࣈྻͰbuildingͷIdΛอ࣋͢ΔͱܾΊͨ ৔߹ͷྫ

    BuildingRepositoryΛfirestoreͰ࣮૷ͨ͠Ϋϥε
  32. ΍ͬͨ͜ͱ - ϑΝΠϧ໊ / Ϋϥε໊͸ػցతʹܾΊΒΕΔ - Entity΍Repository͸ڞ௨ॲཧΛ·ͱΊΒΕΔ ※ ܧঝͰղܾͰ͖Δ෦෼΋͋Δ -

    σʔλͷ࣋ͪํ = EntityͷཁૉͱͰ͖ΔͳΒɺ σʔλܕఆٛ΍convertॲཧ͸ػցతʹܾ·Δ  
  33. ΍ͬͨ͜ͱ Entityͷཁૉ = σʔλͷ࣋ͪํ Λݫक͢Δඞཁ͸ͳ͍ ͕ɺഎܠͱͯ͠ݱঢ়طଘ࣮૷ͷϦϑΝΫλ + ݎ࿚ੑͷ޲্Λ༏ઌతʹ ߦ͓ͬͯΓɺ Entityͷཁૉ

    = σʔλͷ࣋ͪํ ͱܾΊ࣮ͯ૷ਐΊͨͱͯ͠΋ϝϦοτ ͕͋Γར༻Ͱ͖ΔՕॴʹ͸ੵۃతʹ࠾༻  
  34. ΍ͬͨ͜ͱ ओͳม਺ - Ϟσϧ໊ (Building) - छผ (Entity / ValueObject

    / FirstCollection …) - ཁૉ (code:string, address: AddressValueObject)   → ্ه͕ఆٛ͞Ε͍ͯΕ͹ɺޙ͸ػցతʹܾΊΒΕͦ͏ ػցతʹܾΊΔͨΊʹඞཁͳཁૉ
  35. ΍ͬͨ͜ͱ ఆٛϑΝΠϧ͔ΒࣗಈͰιʔεΛ ࡞ΔπʔϧΛར༻ʢࣗ࡞ʣ   ͳʹΛ͔ͨ͠ͱ͍͏ͱ... ※ݱࡏࢼݧӡ༻த..,

  36. ΍ͬͨ͜ͱ   ఆٛϑΝΠϧͷΠϝʔδ ※openapiͷschemaͷϑΥʔϚοτʹଇͬͯهࡌ

  37. ΍ͬͨ͜ͱ ఆٛϑΝΠϧ ʴ npm run gen Ͱ...  

  38. ΍ͬͨ͜ͱ  

  39. ΍ͬͨ͜ͱ  

  40. ΍ͬͨ͜ͱ  

  41. ΍ͬͨ͜ͱ  

  42. ΍ͬͨ͜ͱ ͜ΕΒ͕ࣗಈͰ࡞੒͞Εͯ ޙ͸ݸผ෦෼Λద࣮ٓ૷ʂ  

  43. ΍ͬͨ͜ͱ   Ϟσϧͱͷม׵΋ର৅ͱ͍ͯ͠ΔͷͰɺORMతͳཁૉ΋͋Γ·͕͢ɺ ҎԼ؍఺Λॏཁࢹ͍ͯ͠·͢ ɾORMͷཁૉ͸͋͘·ͰҰ෦ɻݱࡏͷߏ੒ʹ͓͍࣮ͯ૷Λָʹ͢Δ ͨΊͷ࢓૊Έɻ ɾશϞσϧͷม׵ॲཧΛࣗಈੜ੒ͷ࢓૊ΈʹدͤΔඞཁ͸ͳ͍ɻಛʹ ORMͷ෦෼͸ෳࡶͳදݱ͸೉͍͠ͷͰɺݸผ࣮૷΋͘͠͸αϙʔτ తͳཱͪҐஔʹཹΊΔɻ

  44. ΍ͬͨ͜ͱ   ৺͕͚ͨ఺ ঢ়گ͝ͱͷ࢖͍෼͚ ɾΑ͋͘Δγϯϓϧͳύλʔϯ͸ۃྗશࣗಈ ɾෳࡶͳύλʔϯ΍ྫ֎͸Ұ෦αϙʔτ ࣗಈԽͷ࢓ํ ɾॳظςϯϓϨʔτΛ࡞੒ͯͦ͜͠ʹݸผ࣮૷ ɾఆٛϑΝΠϧͱৗʹ࿈ಈ

    ɾ্هͷ૊Έ߹Θͤ
  45. ·ͱΊ  

  46. ·ͱΊ   ੒Ռ ɾDomainModel ͱӬଓԽ૚ͷߏ଄͕ဃ཭͍ͯ͠ͳ͍৔߹ʹ ͸ඇৗʹڧྗͰɺΑΓυϝΠϯͷ࣮૷ʹूதͰ͖Δɻ ※ ͨͩ͠ແཧʹDomainModel ͱӬଓԽ૚ͷߏ଄Λ߹ΘͤΔ

    ͱຊ຤స౗ͱͳΔͷͰͦͷ৔߹ʹ͸ɺར༻͠ͳ͍ or ςϯϓ Ϩʔτͱͯ͠ੜ੒ͨ͠ޙʹݸผमਖ਼͢Δɻ
  47. ·ͱΊ   ෭࢈෺ ɾϑΝΠϧ΍σΟϨΫτϦɺΫϥεͷ໋໊نଇ͕పఈͰ͖Δ ɾ࡞ۀલʹͨ͠ߏ੒ͷ੔ཧ͠ɺͦΕ͕ޮՌతͩͬͨɻ౰ॳ ඞཁ͔΋ͱࢥ͍ͬͯͨϑΝΠϧ΍ΫϥεΛഉআͯ͠ΑΓγϯ ϓϧʹͰ͖ͨ

  48. ·ͱΊ   ࠓޙ ɾӡ༻͢Δ্Ͱ࢖͍ͮΒ͞վળ / ରԠύλʔϯ௥Ճ ɾURLਤͳͲͷՄࢹԽରԠ ɾӡ༻ํ๏ͷվળ

  49. 

  50. Appendix   • DomainModel ← infractructure ͷ ιʔε࣮૷ྔ௿ݮ

  51. Domain Object ͷશମ૾   1. Appendix

  52. Entity / ValueObject   1. Appendix

  53. AbstractDomainObject   1. Appendix

  54. AbstractLooseEntity   1. Appendix

  55. AbstractValueObject   1. Appendix

  56. DIͷ࣮ݱํ๏   1. Appendix

  57. DIͷ࣮ݱํ๏   1. Appendix

  58. Queue Smaple   1. Appendix

  59. Queue Smaple આ໌   1. Appendix ˙2VFVF ɾ2VFVFΛ؅ཧ͢ΔͨΊͷσʔλ ɾॲཧத͔൱͔ɺॲཧ͍ͯ͠ΔUBTL͕ͳʹ͔ͳͲΛ؅ཧ͢Δ

    ˙2VFVF5BTL ɾ2VFVFʹੵ·ΕͨλεΫ ɾॲཧதͱͳΔλΠϛϯάͰ࡟আ͞Εɺॲཧʹࣦഊ͠ϦτϥΠର৅ͱͳͬͨλΠϛϯάͰ࠶ੜ੒͞ΕΔ ɾجຊతʹ͸FORVFVF͞Εͨ೔࣌ॱʹλεΫΛ࣮ߦ͢Δ ɾ༏ઌ౓͕ࢦఆ͞Εͯݴ͏৔߹ʹ͸༏ઌ౓͕ߴ͍ॱʹ࣮ߦ͢Δ ˙2VFVF5BTL3FTVMU ɾ2VFVFͷ࣮ߦ݁ՌΛ؅ཧ͢ΔͨΊͷσʔλ ɾॲཧࣦഊͯ͠ϦτϥΠ͢Δ৔߹ͳͲ͸ɺϦτϥΠ͝ͱʹ݁ՌσʔλΛੜ੒͢Δ ɾ5BTL&OUSZʹ͸࠷ऴతͳ݁ՌͷΈ֨ೲ͢Δ ˙2VFVF&OUSZ ɾ2VFVFλεΫΛ؅ཧ͢Δݩσʔλ ɾλεΫੜ੒࣌ʹຊσʔλ͕ੜ੒͞ΕΔ ɾλεΫ࣮ߦʹඞཁͳ৘ใͱɺ࠷௿ݶͷεςʔλεͷ؅ཧΛߦ͏ ɾ࣮ࡍʹ2VFVF্ͷσʔλભҠΛ؅ཧ͢Δσʔλ͸ผ ˙2VFVF&OUSZ4VCUBTL ɾͭͷ&OUSZΛ࣮ݱ͢ΔͨΊʹෳ਺ͷ5BTLΛ࣮ߦ͢Δඞཁ͕͋Δ৔߹ʹ ɹෳ਺ͷ5BTLΛ4VC5BTLͱͯ͠؅ཧ͢Δ