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

learning-cleanarchitecture-in-go

 learning-cleanarchitecture-in-go

Takashi Matsuyuki

February 28, 2019
Tweet

More Decks by Takashi Matsuyuki

Other Decks in Programming

Transcript

  1. Go ͰֶͿClean Architecture ͓·͚: GoΛֶͿͨΊͷ͓͢͢ΊυΩϡϝϯτ

  2. झࢫ(࠷ॳͷ૝ఆ) Go 㲗 CA GoΛ௨ͯ͠CAͷཧղΛਂΊΔ CAΛ௨ͯ͠GoͷཧղΛਂΊΔ

  3. झࢫ(ࢿྉ࡞ͬͨ݁Ռ) Go → CA GoΛ௨ͯ͠CAͷཧղΛਂΊΔ CAΛ௨ͯ͠GoͷཧղΛਂΊΔ ޙΖʹ͓·͚Λ͚ͭ·ͨ͠

  4. ΞδΣϯμ • Clean Architectureͱ͸ʁ • SOLIDݪଇ • CAͷϨΠϠʔߏ଄ • ϨΠϠʔͷڥքɺӽڥ

    • GoͰCA࠾༻͢ΔͱͲ͏ʁ • GoΛษڧ͢ΔͳΒ͜ΕಡΜͲ͚
  5. αϯϓϧΞϓϦέʔγϣϯ TODOΞϓϦΛ૝ఆ User৘ใͱTask৘ใΛฦ͢APIαʔό ϦϙδτϦ: https://github.com/applepine1125/learning- cleanarchitecture-in-go ઈࢍվળத 13ड෇த

  6. ෮श ΞʔΩςΫνϟͱ͸ʁ

  7. ΞʔΩςΫνϟͷ໨త ॏཁͳํ਑ʹ஫ྗ͠ɺৄࡉͷܾఆΛ஗ΒͤΔ →มߋʹॊೈͳΞϓϦέʔγϣϯΛ࡞Δ ϏδωεϩδοΫ ํ਑ ϑϨʔϜϫʔΫɺυϥΠό ৄࡉ ॏཁ౓ େখ ଐਓੑ

    େখ
  8. ෮श Clean Architectureͱ͸ʁ

  9. ͳΜ͔͜Μͳਤͷ΍ͭ

  10. CAͷ໨తɺಛੑ • ϑϨʔϜϫʔΫඇґଘ • ςετՄೳ • UIඇґଘ • σʔλϕʔεඇґଘ •

    ֎෦ΤʔδΣϯτඇґଘ →ͭ·Γৄࡉͱํ਑Λ੾Γ཭͠΍͍͢
  11. CAΛߏ੒͢Δཁૉ • Ϟδϡʔϧείʔϓʹ͓͚Δ5ݪଇ • ίϯϙʔωϯτείʔϓʹ͓͚Δ6ݪଇ • ϨΠϠʔ • ֤ϨΠϠʔͷҙຯɺ੹຿ •

    ϨΠϠʔͷڥքͷҾ͖͔ͨɺӽڥͷ͔ͨ͠
  12. CAΛߏ੒͢Δݪଇ • Ϟδϡʔϧείʔϓ (SOLIDݪଇ) • ୯Ұ੹೚ͷݪଇ (SRP) • ΦʔϓϯɾΫϩʔζυͷݪଇ (OCP)

    • Ϧείϑͷஔ׵ݪଇ (LSP) • ΠϯλʔϑΣʔε෼཭ͷݪଇ (ISP) • ґଘؔ܎ٯసͷݪଇ (DIP) ← Ұ൪ॏཁ
  13. CAΛߏ੒͢Δݪଇ • ίϯϙʔωϯτείʔϓ (ڽूੑ) • ࠶ར༻ɾϦϦʔε౳Ձͷݪଇ (REP) • ด࠯ੑڞ௨ͷݪଇ (CCP)

    • શ࠶ར༻ͷݪଇ (CRP)
  14. CAΛߏ੒͢Δݪଇ • ίϯϙʔωϯτείʔϓ (ؔ࿈ੑ) • ඇ॥؀ґଘؔ܎ͷݪଇ (ADP) • ҆ఆґଘͷݪଇ (SDP)

    • ҆ఆ౓ɾந৅౓౳Ձͷݪଇ (SAP)
  15. CAΛߏ੒͢Δݪଇ • Ϟδϡʔϧείʔϓ • DIP ←Ұ൪ॏཁ • Ϟδϡʔϧείʔϓ • SRP,

    OCP, LSP, ISP • ίϯϙʔωϯτείʔϓ(ڽूੑ) • REP, CCP, CRP • ίϯϙʔωϯτείʔϓ(ؔ܎ੑ) • ADP, SDP, SAP આ໌͠·͢ ࣗश
  16. SOLIDݪଇ

  17. ୯Ұ੹೚ͷݪଇ “Ϟδϡʔϧ͸ͨͬͨҰͭͷΞΫλʔʹରͯ͠ ੹຿Λෛ͏΂͖Ͱ͋Δ”

  18. ୯Ұ੹೚ͷݪଇ ϙΠϯτ • มߋ͢Δཧ༝͕ඞͣڞ௨Ͱ͋Ε͹ɺ ڞ௨Ϟδϡʔϧͱͯ͠੾Γग़ͯ͠΋ྑ͍ ٯʹݴ͏ͱͲ͏ͳΔʁ

  19. ΦʔϓϯɾΫϩʔζυͷݪଇ “ߏ੒ཁૉ͸֦ுʹରͯ͠͸։͍͍ͯͯɺमਖ਼ʹ ରͯ͠͸ด͍ͯ͡ͳ͚Ε͹ͳΒͳ͍”

  20. ΦʔϓϯɾΫϩʔζυͷݪଇ ϙΠϯτ • interfaceʹΑͬͯ৽͍͠ػೳΛ؆୯ʹ औΓೖΕΔ͜ͱ͕Ͱ͖Δ • ͋ΔϞδϡʔϧ΁ͷมߋͷӨڹ͕ଞͷ Ϟδϡʔϧ΁Өڹ͠ͳ͍

  21. Ϧείϑͷஔ׵ݪଇ “SܕͷΦϒδΣΫτͷo1ͷ֤ʑʹɺରԠ͢ΔTܕͷΦϒδΣΫτo2͕̍ͭ ଘࡏ͠ɺTΛ࢖ͬͯఆٛ͞ΕͨϓϩάϥϜPʹରͯ͠o2ͷ୅ΘΓʹo1Λ ࢖ͬͯ΋PͷৼΔ෣͍͕มΘΒͳ͍৔߹S͸Tͷ೿ੜܕͰ͋Δͱݴ͑Δ”

  22. Ϧείϑͷஔ׵ݪଇ ϙΠϯτ • ϞδϡʔϧΛஔ׵Մೳͳঢ়ଶʹ͢Δ ΦʔϓϯɾΫϩʔζυͷݪଇͷ֦ுੑͷ಺༰ͱҰॹ

  23. ΠϯλʔϑΣʔε෼཭ͷݪଇ ϙΠϯτ • ຊདྷඞཁͱ͠ͳ͍΋ͷʹ·Ͱґଘ͠ͳ͍Α͏ ʹɺΠϯλʔϑΣʔεΛڬΜͰૄ݁߹ʹ͢Δ

  24. ґଘؔ܎ٯసͷݪଇ • interfaceΛ༻͍ͯґଘؔ܎Λٯసͤ͞Δ 6TFDBTF 3FQPTJUPSZ 42-)BOEMFS %*1ద༻લ %*1ద༻ޙ 6TFDBTF 3FQPTJUPSZ

    42-)BOEMFS 3FQPTJUPSZ *OUFSGBDF 42-)BOEMFS *OUFSGBDF
  25. ґଘؔ܎ٯసͷݪଇ ϙΠϯτ • มԽ͠΍͍͢ϞδϡʔϧΛࢀরɺܧঝɺ ΦʔόʔϥΠυɺ໊ࢦ͠Ͱࢀর͠ͳ͍ →CAͰϨΠϠʔΛ·͍ͨͰϞδϡʔϧΛ ࢀর͢Δͱ͖ʹ௒ॏཁʂ

  26. CAͷ໨తɺಛੑ • ϑϨʔϜϫʔΫඇґଘ • ςετՄೳ • UIඇґଘ • σʔλϕʔεඇґଘ •

    ֎෦ΤʔδΣϯτඇґଘ →ݪଇΛར༻͢Ε͹࣮ݱͰ͖ͦ͏ʁ վΊͯɾɾɾ
  27. CAͷϨΠϠʔߏ଄

  28. CAͷ֤ϨΠϠʔ • ΤϯςΟςΟ • Ϣʔεέʔε • ΠϯλʔϑΣʔεΞμϓλ • ϑϨʔϜϫʔΫɺυϥΠό

  29. • ΤϯςΟςΟ • Ϣʔεέʔε • ΠϯλʔϑΣʔεΞμϓλ • ϑϨʔϜϫʔΫɺυϥΠό ϙΠϯτ ୯ํ޲ґଘͰ͋Δ

    CAͷ֤ϨΠϠʔ ґଘ͞Ε͍ͯΔ ԁͷத ԁͷ֎ ґଘ͍ͯ͠Δ
  30. ΤϯςΟςΟ • Ϗδωεࣗମͷ࠷ॏཁϧʔϧΛ·ͱΊͨ΋ͷ • γεςϜ͕webΞϓϦέʔγϣϯͱ͍͏ܗଶ Ͱͳ͔ͬͨͱͯ͠΋ଘࡏ͢Δ৘ใ

  31. ΤϯςΟςΟ w ۀ຿ʹඞཁͳ৘ใ͕ೖͬͨߏ଄ମ w ඞཁͳ৘ใΛੜ੒͢ΔͨΊͷϝιου ྫTSDQLHEPNBJOVTFSHP

  32. Ϣʔεέʔε • ΞϓϦέʔγϣϯݻ༗ͷϏδωεϧʔϧ ΛؚΉ • ΤϯςΟςΟͷϏδωεϩδοΫΛ૊Έ߹Θ ͤͯ࢖༻ɺ੍ޚ͢Δ

  33. Ϣʔεέʔε ྫTSDQLHVTFDBTFUBTLJOUFSBDUPSHP w σʔλͷऔಘ SFQPTJUPSZ ͸JOUFSGBDFΛૠ͢ w ӬଓԽ͢ΔͨΊʹUBTLͷ৘ใΛ࢖ͬͯΞϓϦέʔγϣϯݻ༗ͷ σʔλͷܗʹม׵

  34. ΠϯλʔϑΣʔεΞμϓλ • ϢʔεέʔεɺΤϯςΟςΟ͔ΒDB΍ϑϨʔ ϜϫʔΫͳͲ͕࢖͏ϑΥʔϚοτ΁σʔλΛ ม׵͢Δ(ͦͷٯ΋વΓ) • ೖྗɺग़ྗɺӬଓԽʹؔ͢ΔϞδϡʔϧ͕ॴ ଐ͢Δ

  35. ΠϯλʔϑΣʔεΞμϓλ ྫTSDQLHJOUFSGBDFUBTLSFQPTJUPSZHP w ӬଓԽͷͨΊʹΫΤϦΛൃߦ͍ͯ͠Δ

  36. ϑϨʔϜϫʔΫɺυϥΠό • ϑϨʔϜϫʔΫ΍πʔϧͳͲͷίʔυͰ ߏ੒͞Ε͍ͯΔ • WebͷUIͳͲ΋͜͜

  37. ϑϨʔϜϫʔΫɺυϥΠό ྫTSDQLHJOGSBTRMIBOEMFSHP w EBUBCBTFTRMϥΠϒϥϦΛ࢖༻ w ԼҐ૚ͱ઀ଓ͠΍͍͢Α͏ʹXSBQ͢Δ

  38. ϨΠϠʔͷڥքɺӽڥ

  39. ϨΠϠʔͷڥքɺӽڥ ڥքͷҾ͖͔ͨ • γεςϜΛίϯϙʔωϯτʹ෼ׂ͢Δ • ݪଇɺϧʔϧΛݩʹ֤ϨΠϠʔʹ෼ྨ͢Δ • ґଘؔ܎ͷઢΛҾ͍ͯΈΔ

  40. ϨΠϠʔͷڥքɺӽڥ ӽڥͷ͔ͨ͠ • DIPΛ࢖ͬͯґଘͷ޲͖Λίϯτϩʔϧ͢Δ 3FQPTJUPSZ 6TFDBTF 6TFDBTF 3FQPTJUPSZ JOUFSGBDF 3FQPTJUPSZ

    *OUFSGBDF"EBQUFS 6TFDBTF %*1ద༻લ %*1ద༻ޙ
  41. ϨΠϠʔͷڥքɺӽڥ ӽڥͷ͔ͨ͠ TSDQLHVTFDBTFVTFSJOUFSBDUPSHP TSDQLHJOUFSGBDFVTFSDPOUSPMMFSHP ܕ͸VTFDBTF6TFS3FQPTJUPSZ ࣮ࡍʹೖΕͯΔͷ͸EBUBCBTF6TFS3FQPTJUPSZ

  42. GoͰCA࠾༻͢ΔͱͲ͏ʁ

  43. GoͰCA࠾༻͢ΔͱͲ͏ʁ ϥΠϒϥϦʹґଘ͗͢͠ͳ͍ • αʔυύʔςΟͷϥΠϒϥϦ࢖͍͗ͣ͢ɺ͋͘ ·Ͱwrapperͱͯ͠࢖͏ఔ౓ʹཹΊΔจԽ͕ ͋Δ • infra૚ҎԼ΁ͷ֎෦ίϯϙʔωϯτͷӨڹ͕ গͳ͍ →֎෦ίϯϙʔωϯτͷมߋʹͭΑ͍

  44. GoͰCA࠾༻͢ΔͱͲ͏ʁ MockͭΒ͍໰୊ • CAΛ࠾༻͢Δͱinterface͍ͬͺ͍ੜ͑Δ • ࣗ࡞interface͸goMockͳͲͰMockੜ੒ HPMBOHNPDL IUUQTHJUIVCDPNHPMBOHNPDL

  45. GoͰCA࠾༻͢ΔͱͲ͏ʁ ඪ४ύοέʔδΛMock͢Δʹ͸ɾɾɾʁ • ςετ͍ͨ͠ϥΠϒϥϦͷinterface, ߏ଄ମͱ ϝιου͕Ұக͢ΔinterfaceΛखݩͰ࡞Γ goMockͰMockੜ੒ ͩΔ͍

  46. CAͷ·ͱΊ

  47. CA·ͱΊ ”ਤͷԁ͸ɺ֓ཁΛࣔͨ͠΋ͷͰ͋Δɻ͕ͨͬͯ͠ɺ͜ͷ̐ͭ Ҏ֎ʹ΋ඞཁͳ΋ͷ͸͋ΔͩΖ͏ɻͨͩ͠ɺґଘੑͷϧʔϧ ͸ৗʹద༻͞ΕΔ” ”νʔϜͷن໛΍ϝϯόʔͷεΩϧ΍ιϦϡʔγϣϯͷෳࡶ ͞ɺͦͯ࣌ؒ͠ͱ༧ࢉͷ੍໿ͳͲΛߟྀ͠Α͏” 3PCFSU$.BSUJO

  48. CA·ͱΊ • νʔϜ಺Ͱ߹ҙ͕औΕ͍ͯΕ͹ਖ਼௚ͳΜͰ ΋͍͍ͱࢥ͍ͬͯΔ • ԿΛୡ੒͍͔ͨ͠Λݟਾ͑ɺઌਓ͕࢒ͨ͠ ݪཧݪଇ΍νʔϜͷܦݧɺγεςϜͷಛੑɺ υϝΠϯ஌ࣝͳͲΛϑϧʹ׆༻ͯ͠ৗʹద ੾ͳߏ଄ʹͳΔΑ͏౒ྗ͢Δ

  49. CA͓͢͢ΊυΩϡϝϯτ w $MFBO"SDIJUFDUVSFɹୡਓʹֶͿιϑτ΢ΣΞͷߏ଄ͱઃܭ IUUQTXXXBNB[PODPKQEQ#'4#)47SFGEQLJOEMFSFEJSFDU @FODPEJOH65'CULS w ࣮ફΫϦʔϯΞʔΩςΫνϟ IUUQTOSTMJCDPNDMFBOBSDIJUFDUVSF w $MFBO"SDIJUFDUVSFͰ"1*4FSWFSΛߏஙͯ͠ΈΔ

    IUUQTRJJUBDPNIJSPUBLBOJUFNTDGBDDBF
  50. GoΛษڧ͢ΔͳΒ͜ΕಡΜͲ͚

  51. GoΛษڧ͢ΔͳΒ͜ΕಡΜͲ͚ • ॳ৺ऀ w "5PVSPG(P IUUQTHPUPVSKQBQQTQPUDPNXFMDPNF w 1SPHBUF IUUQTQSPHDPNMBOHVBHFTHP

  52. GoΛษڧ͢ΔͳΒ͜ΕಡΜͲ͚ • ॳ৺ऀ~தڃऀ w (PݴޠʹΑΔ8FCΞϓϦέʔγϣϯ։ൃ w (PͰଞ෼໺ͷษڧΛ͢Δ (PͳΒΘ͔ΔγεςϜϓϩάϥϛϯά (PݴޠͰͭ͘ΔΠϯλϓϦλ 3FBM8PSME)551

    8SJUJOHdJO(Pܥ
  53. GoΛษڧ͢ΔͳΒ͜ΕಡΜͲ͚ • தڃऀ~্ڃऀ w (PݴޠʹΑΔฒߦॲཧ w ϓϩάϥϛϯάݴޠ(P w ΈΜͳͷ(Pݴޠʲݱ৔Ͱ࢖͑Δ࣮ફςΫχοΫʳ w

    &⒎FDUJWF(P ӳޠ൛ɿIUUQTHPMBOHPSHEPDF⒎FDUJWF@HPIUNM ೔ຊޠ൛IUUQHPMBOHKQF⒎FDUJWF@HP
  54. GoΛษڧ͢ΔͳΒ͜ΕಡΜͲ͚ GoDocಡΊ

  55. ͓ΘΓ