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

DDD-4layers

nozawa
December 22, 2024

 DDD-4layers

ジャンケンアドカレを読んでDDD×4層に入門したので、
ありがちな悪いアーキテクチャと比較しながら理解をアウトプットしました。

nozawa

December 22, 2024
Tweet

More Decks by nozawa

Other Decks in Programming

Transcript

  1. ໰୊ҙࣝᶃΞʔΩςΫνϟ w 4QSJOHGSBNFXPSL .7$  τϥϯβΫγϣϯεΫϦϓτ  w ϨΠϠʔ͕શ͘෼͔Βͳ͍ Αͬͯɺґଘͷ޲͖͕੔ཧ͞Εͳ͍

    w ೝ஌ෛՙ͕ߴ͍ ΞʔΩςΫνϟ͕͋ΔΑ͏Ͱͳ͍ͨΊ࡞ΓΛ஌ ΔͨΊʹ࣮૷Λ௥͏͔͠ͳ͍ ͖͔͚ͬ TSDNBJOKBWB cDPOUSPMMFS cFOUJUZ cTFSWJDF cGPSN cSFQPTJUPSZ cNPEFM cFYDFQUJPO cʜ TSDNBJOXFC cQBHFTը໘ cʜ
  2. ໰୊ҙࣝᶄ4FSWJDF w υϝΠϯ஌͕ࣝ݁ू͍ͯ͠Δ ӬଓԽͷ੍໿ɺϧʔϧ΋͜͜ w ࠷΋Մಡੑ͕௿͍ ಛʹ%50ͷॲཧ͸ͪΐͬͱͨ͠6*ͷؔ৺͝ͱͰ ͳͷʹΫϥεͷେ൒Λ઎Ί͍ͯͨΓ͢Δ w ෳࡶੑͱ΋ͬͱઓ͑Δͱײ͡Δ

    ͖͔͚ͬ !4FSWJDF QVCMJDDMBTT4FSWJDF\ !"VUPXJSFE QSJWBUF0UIFS4FSWJDFPUIFS4FSWJDF !"VUPXJSFE QSJWBUF3FQPTJUPSZSFQPTJUPSZ  QVCMJD'PSNTFU'PSN%BUF 'PSNGPSN \ GPSNTFU.PEFM  SFQPTJUPSZHFU%BUB#Z*E GPSNHFU*E  ݸผͰ6*౎߹ͷͭΊͭΊ GPSNTFU*UFN  SFUVSOGPSN ^ QVCMJD'PSNJOTFSU 'PSNGPSN \ JOTFSU༻ͷFOUJUZ ʹͭΊͭΊ .PEFMNPEFMOFX.PEFM  JEͷ࠾൪΋͜͜Ͱ͢Δ NPEFMTFU*E SFQPTJUPSZHFU/FYU*E  ^ ^
  3. ໰୊ҙࣝᶅ&OUJUZ .PEFM w ςʔϒϧσʔλήʔτ΢ΣΠ 3%#ͷςʔϒϧͱରɻ&OUJUZग़ྗ͘Μతͳ&YDFMϚ Ϋϩ͕͋ͬͨΓ͢Δ w +0*/ͷଟ͍ෳࡶͳΫΤϦ͸.PEFMΛ ࢖͏ ςʔϒϧσʔλήʔτ΢ΣΠͷฐ֐ͩͱࢥ͏ɻ001త

    Ͱͳ͘ઃܭΛݟ͑ʹ͘͘͢ΔݪҼͩͱݸਓతʹ͸ࢥͬ ͍ͯΔ w &OUJUZͱ.PEFM͕ͨ͘͞Μ͋Δ ςʔϒϧ ΧϥϜͷ6*දࣔͷͨΊʹ&OUJUZΛܧঝͯ͠ ࠩ෼ͷΧϥϜ଍ͨ͠.PEFM͕࡞ΒΕ͍ͯΔ͜ͱ͕ཧ ༝ͩͱࢥ͍ͬͯΔ ͖͔͚ͬ !&OUJUZ QVCMJDDMBTTB5CMJNQMFNFOUT4FSJBMJ[BCMF\ !*E !$PMVNO OBNFlJEz  QSJWBUFMPOHJE  ҎԼɺશϑΟʔϧυʹର͢Δ4FUUFS (FUUFS QVCMJDMPOHHFU*E \ SFUVSOJE ^ QVCMJDWPJETFU*E MPOHJE \ UIJTJEJE ^ ^
  4. ໰୊ҙࣝᶆ3FQPTJUPSZ w ΦϨΦϨ+%#$ϥούʔ w σʔλࢦ޲ΦϒδΣΫτࢦ޲ &OUJUZର3FQPTJUPSZ w υϝΠϯ஌͕ࣝ࿙Εग़͍ͯΔ VQEBUFʓʓͰݸผΞοϓσʔτͨ͠Γෳࡶͳ࢓༷ Λෳࡶͳ42-Ͱؤுͬͯղܾ͢ΔͳͲ

    w SFQPTJUPSZ ӬଓԽͷӅṭ Ͱ͸ͳ͍ Α͏ͳ 42-ϑΝΠϧΛಡΜͰ͍Δ͠Լखͨ͠Β42-Λ௚઀ ॻ͍͍ͯΔɻ%"0ύλʔϯ ͖͔͚ͬ !3FQPTJUPSZ QVCMJDDMBTT3FQPTJUPSZ\ !"VUPXJSFE QSJWBUF+ECD.BOBHFSKECD.BOBHFS  QVCMJD-JTU.PEFMHFU)PHF#Z*E .PEFMN \ SFUVSOKECD.BOBHFSHFU3FTVMU  CFBODMBTT lTRMQBUIz N  ^ QVCMJDJOUJOTFSU .PEFMN \ SFUVSOKECD.BOBHFSJOTFSU lTRMQBUIz N  ^ ^
  5. ͖͔͚ͬ·ͱΊ w ϨΠϠʔ͝ͱͷ࠴຿ͱґଘΛ͸͖ͬΓͤͯ͞ೝ஌తෛՙΛ௿͍ͨ͘͠ w Ϋϥε͝ͱͷ࠴຿Λ͸͖ͬΓ͍ͤͨ͞ FOUJUZͰ͍͔ۤ͠ΒNPEFM࢖͏ͱ͔ݏ  w ΋ͬͱΦϒδΣΫτࢦ޲Λಥ͖٧Ί͍ͨ ݸਓతཉٻ

     w Ͱ΋Ͳ͏΍ͬͯίʔυʹམͱͤ͹͍͍ͷʁ ɹɹ ɹɹ͡ΌΜ͚ΜΞυϕϯτΧϨϯμʔʹग़ձ͍ ɹɹܰྔ%%%ͱ૚ ϨΠϠʔυΞʔΩςΫνϟ ͕ྑͦ͞͏ͩͱؾͮ͘ IUUQTRJJUBDPNBEWFOUDBMFOEBSKBOLFO
  6. ྑ͍ͱײ͡Δ໘ w ύοͱݟͯ૚ͱΘ͔Δ͜ͱͰೝ ஌ෛՙ͸Լ͕ͬͨͱࢥ͏ w ϨΠϠʔ͝ͱʹ4FSWJDFΛ෼ׂͰ ͖ͨ w 4FSWJDF΍&YDFQUJPOΛ૚Ͱ੾Γ ෼͚ͯߟ͑ΒΕΔΑ͏ʹͳͬͨ

    ྫ͑͹SFRVFTUQBSBNTʹର͢Δྫ֎͸ "QQMJDBUJPO૚ɺυϝΠϯͷ੍໿্ͷྫ֎͸ EPNBJO૚ɺͳͲ ॴײ ΞʔΩςΫνϟฤ TSDNBJOKBWB cBQQMJDBUJPOTFSWJDF cEPNBJO cNPEFM cTFSWJDF cFYDFQUJPO cJOGSBTUSVDUVSF cSFQPTJUPSZ cQSFTFOUBUJPO cFYDFQUJPO cBQJ cEUP
  7. ѱ͍ͱײ͡Δ໘ ࢑ఆ w 4FSWJDFͷ࠴຿ͷ෼཭ʹ೰Ή ͜Ε͸%PNBJO4FSWJDF "QQMJDBUJPO4FSWJDF  w ϨΠϠʔؒͷσʔλͷड͚౉͠ ʹ೰Ή

    BQQMJDBUJPO4FSWJDFͰQSFTFOUBUJPO૚ͷEUPʹ σʔλΛ٧ΊΔʁґଘͷ޲͖߹ͬͯΔ ॴײ ΞʔΩςΫνϟฤ TSDNBJOKBWB cBQQMJDBUJPOTFSWJDF cEPNBJO cNPEFM cTFSWJDF cFYDFQUJPO cJOGSBTUSVDUVSF cSFQPTJUPSZ cQSFTFOUBUJPO cFYDFQUJPO cBQJ cEUP
  8. ྑ͍ͱײ͡Δ໘ w &OUJUZ͕ΦϒδΣΫτࢦ޲͍ͯ͠Δײ FOUJUZͷৼΔ෣͍ͱͯ͠ఏڙ͞ΕΔϝιου͔Βঢ়ଶ Λมߋ TFUUFSͷ࢖༻ Λ͢Δͷ͸001ͩͳͱࢥ͏ w &OUJUZΛಡΊ͹͍͍ͱ͍͏҆৺ײ อकۀ຿ʹෳࡶͳΫΤϦͷղಡؚ͕·Ε͍ͯͳ͍͜ͱ

    ͕อূ͞Ε͍ͯΔ͜ͱ͸ਫ਼ਆӴੜ্ྑ͍ w $234ͰNPEFMΫϥε ෳࡶͳ SFQPTJUPSZ͔Βͷղ์ SFQPTJUPSZΛԚ͢ݪҼ͸ෳࡶͳRVFSZͰӬଓԽ૚Ͱͳ ͘6*ͷ౎߹͔ͩΒׂΓ੾ͬͯRVFSZ4FSWJDFΛ࡞Δͱ͍ ͏ߟ͑ํ͸ଥ౰ੑ͕͋Δͱࢥ͏ ॴײ %%%ฤ @Entity class Task { @Id @GeneratedValue private Long id; private TaskStatus taskStatus; private String name; private int postponeCount; private static final int POSTPONE_MAX_COUNT = 3; /* * ίϯετϥΫλɿ ΤϯςΟςΟ࡞੒࣌ͷෆม৚݅Λදݱ */ public Task(String name, LocalDate dueDate) { if (name == null || dueDate == null) { throw new IllegalArgumentException("ඞਢ"); } this.name = name; this.taskStatus = TaskStatus.UNDONE; this.postponeCount = 0; } }
  9. ѱ͍ͱײ͡Δ໘ ࢑ఆ w ΠϯϐʔμϯεϛεϚονͷղফ ͕໘౗͍͘͞ w ू໿ͷ੾Γํ͕%#ͷϞσϦϯά ʹҾͬுΒΕΔ w σʔλΞΫηεͷύϑΥʔϚϯε

    ѱͦ͏ ॴײ %%%ฤ !3FQPTJUPSZ QVCMJDDMBTT3FQPTJUPSZ*NQMJNQMFNFOUT3FQPTJUPSZ\ !"VUPXJSFE 1SJWBUF.PPE.BQQFSQPTU.BQQFS  QVCMJDWPJETBWF 1PTUQPTU \ QPTU.BQQFSTBWF NPPE  -JTU4USJOHJETNPPEHFU.PPE*E  TUSFBN   fi MUFS MJLF NPPEHFU-JLFT DPOUBJOT MJLF  DPMMFDU $PMMFDUPSTUP-JTU   -JTU-JLFMJLFTMJLF.BQQFS fi OE#Z*E JET  MJLFTGPS&BDI M\ MJLF.BQQFSTBWF M  ^  ^ ^