Slide 1

Slide 1 text

ࠓͦ͜ཧղ͢Δ %*ʢ%FQFOEFODZ*OKFDUJPOʣ ʮ1)1FS,BJHJʯ IUUQTGPSUFFKQQIQFSLBJHJ

Slide 2

Slide 2 text

ܰࣗ͘ݾ঺հ

Slide 3

Slide 3 text

ࣗݾ঺հ • ࢁాɹ঵ʢ΍·ͩɹ͠ΐ͏ʣ • Twitter: @rukiadia • ίωώτגࣜձࣾͰόοΫΤϯυΤϯδχΞΛ͍ͯ͠·͢ • Railsͷੈք͔ΒPHPͷੈքʹ΍͖ͬͯ·ͨ͠ɻ

Slide 4

Slide 4 text

ຊ೔ͷൃදͰ໨ࢦ͢ΰʔϧ

Slide 5

Slide 5 text

ຊ೔ͷΰʔϧ %*ʹೖ໳Ͱ͖ͨঢ়ଶ ʢଏʹݴ͏ɺ׬શʹཧղͨ͠ঢ়ଶʣ Λ໨ࢦ͍͖ͯ͠·͢ɻ

Slide 6

Slide 6 text

͓࿩͢Δ͜ͱ w %*ͷ֓࿦ w %*ʹର͢Δޡղ

Slide 7

Slide 7 text

ͦΕͰ͸ɺຊฤελʔτͰ͢ɻ

Slide 8

Slide 8 text

%*ͷ֓࿦

Slide 9

Slide 9 text

DIͷ֓࿦ ͦ΋ͦ΋%*ͱ͸Կͳͷ͔ʁ ղઆΛ͍͔ͭ͘ಡΜͰΈͯ΋ ϐϯͱ͜ͳ͍͠ɺ݁ہΑ͘෼͔Βͳ͍ɻ

Slide 10

Slide 10 text

DIͷ֓࿦ %*ͱ͸ํ๏࿦Ͱ͢

Slide 11

Slide 11 text

“Dependency Injection Principles, Practices, and Patterns” Part 1 Putting Dependency Injection on the map “Dependency Injection is a set of software design principles and patterns that enables you to develop loosely coupled code.” 11 DIͷ֓࿦

Slide 12

Slide 12 text

%*ͱ͸ʁ w อकੑͷߴ͍ίʔυΛॻ͘͜ͱΛ໨తͱͨ͠ɺιϑτ΢ΣΞઃܭݪଇ ͱύλʔϯͷηοτɻ w ιϑτ΢ΣΞઃܭݪଇͷͭͱͯ͠ɺΏΔ΍͔ͳ݁߹ʢૄ݁߹ʣΛ࡞ ΔΞϓϩʔν͕͋Δɻ w %*͸ʮૄ݁߹ͷ࣮ݱΛ໨తͱͨ͠σβΠϯύλʔϯʯͱ͍͏ͷ͕ద੾ ͳදݱͱݴ͑Δɻ w ʮ%*ύλʔϯʯͱݺশ͍ͯ͠Δਓ΋͍Δɻ

Slide 13

Slide 13 text

%*͕ੜ·Εͨʢఆٛ͞ΕͨʣܦҢʹ ৮Ε͓͖ͯ·͢ɻ DIͷ֓࿦

Slide 14

Slide 14 text

%*͕ఆٛ͞ΕΔʹࢸͬͨܦҢ

Slide 15

Slide 15 text

%*ͷىݯΛḷΔ w ىݯ͸+BWBͷίϛϡχςΟ w ೥ʙ೥ࠒʹ.BSUJO'PXMFS͕ఆٛͨ͠ݴ༿ w ˞ࢀߟจݙ͸εϥΠυͷ຤ඌʹࡌͤ·͢ɻ w ʮ*OWFSTJPOPG$POUSPMʢ*P$ʣʯͱ͍͏֓೦͕౰࣌ଘࡏ͓ͯ͠Γɺ ͦͷ֓೦Λ۩ମԽ͢Δݴ༿ͱͯ͠ɺʮ%FQFOEFODZ*OKFDUJPOʯ͕ఆ ٛ͞Εͨɻ

Slide 16

Slide 16 text

*P$ʢ੍ޚͷٯసʣͱ͸Կͳͷ͔ʁ DIͷىݯ

Slide 17

Slide 17 text

*P$ʢ੍ޚͷٯసʣͦͷ w ϓϩάϥϜʹ͓͍ͯɺʮݺͼग़͢ଆʯͱʮݺͼग़͞ΕΔଆʯ͕ैདྷͷ ϓϩάϥϜͱٯʹͳΔݱ৅Λࢦ͢ɻ w δϣʔΫͱͯ͠ͷҙຯ߹͍ͰʮϋϦ΢ουͷݪཧʯͱݺ͹ΕΔ͜ͱ΋ ͋Δɻ w ʮզʑΛݺͿͳɻզʑ͕ݺͿɻʯ w ͚ͩͨ͘ݴ͍ํΛ͢ΔͱɺʮඞཁʹͳͬͨΒɺͪ͜Β͔Βݺͼ· ͢ɻʯͱ͍͏ҙຯ߹͍ʹͳΔɻ

Slide 18

Slide 18 text

*P$ʢ੍ޚͷٯసʣͦͷ w ϑϨʔϜϫʔΫΛ࢖ͬͨΞϓϦέʔγϣϯ։ൃʹ౰ͯ͸·ΔಛੑͰ΋ ͋Δɻ w ։ൃऀ͕ఆٛͨ͠ϝιου͕ΞϓϦέʔγϣϯίʔυ͔Β௚઀ݺ͹ ΕΔ͜ͱ͸΄΅ແ͍ɻݺ͹ΕΔλΠϛϯά͸ϑϨʔϜϫʔΫଆʹҕ ͶΒΕ͍ͯΔɻ w ʮඞཁʹͳͬͨΒɺͪ͜Β͔Βݺͼ·͢ɻʯͱ͍͏੍ޚ

Slide 19

Slide 19 text

จষͩͱ఻ΘΓʹ͍͘ͷͰ ίʔυྫΛݟͯΈ·͠ΐ͏ɻ IoCʢ੍ޚͷٯసʣ

Slide 20

Slide 20 text

ྫɿ$BLF1)1ͷϢχοτςετ class ArticlesTableTest extends TestCas e { public $fixtures = ['app.Articles'] ; public function setUp( ) { parent::setUp() ; $this->Articles = TableRegistry::getTableLocator()->get('Articles') ; } public function tearDown( ) { TableRegistry::clear() ; } } هࣄͷݕࡧ΍ফڈΛߦ͏ .PEFMςετͷԼ஍

Slide 21

Slide 21 text

ྫɿ$BLF1)1ͷϢχοτςετʢ੍ޚͷٯసʣ class ArticlesTableTest extends TestCas e { public $fixtures = ['app.Articles'] ; public function setUp() { parent::setUp() ; $this->Articles = TableRegistry::getTableLocator()->get('Articles') ; } public function tearDown( ) { TableRegistry::clear() ; } } ४උɺޙ࢝຤ɺςετ࣮ߦͷλΠϛϯάΛ ϑϨʔϜϫʔΫ͕Ѳ͍ͬͯΔ

Slide 22

Slide 22 text

Ұ୴ɺ%*ͷ࿩ʹ໭Γ·͢ɻ IoCʢ੍ޚͷٯసʣ

Slide 23

Slide 23 text

*P$ͱ%*ͷؔ܎ w *P$͸ʮ੍ޚͷٯసʯ w %*͸ݴ͍׵͑Ε͹ʮґଘؔ܎ʹର͢Δ੍ޚͷٯసʯͱදݱͰ͖Δɻ w %*Λʮ੍ޚͷٯసʯͱݴ͍੾ͬͯ΋ӕʹ͸ͳΒͳ͍͕ɺ%**P$ͱ͢ Δʹ͸*P$ͱ͍͏දݱ͕แׅత͗͢Δɻ w ʮґଘؔ܎ʹର͢Δ੍ޚͷٯసʯΛͭͷύλʔϯͱͯ͠ଊ͑Δͨ Ίɺ%FQFOEFODZ*OKFDUJPOͱ͍͏໋໊͕ੜ·Εͨɻ

Slide 24

Slide 24 text

Inversion of Control Dependency Injection DIͷ֓࿦

Slide 25

Slide 25 text

ϦΞϦςΟͷ͋ΔίʔυΛ·͑ͯ͡ આ໌Λଓ͚·͢ɻ DIͷ֓࿦

Slide 26

Slide 26 text

ྫɿ&NBJMΛૹ৴͢ΔΫϥε class EmailSender { private $mailClient ; public function init( ) { // ʮAmazon Simple Email ServiceʯΛ࢖༻ͯ͠ϝʔϧΛѻ͏Ϋϥε $this->mailClient = new AwsSes( ) } public function sendRegisterMail($address, $text ) { $this->mailClient->send($address, $text) ; } } Ұݟɺ໰୊ͳͦ͞͏ʹݟ͑Δ

Slide 27

Slide 27 text

ྫɿ&NBJMΛૹ৴͢ΔΫϥε class EmailSender { private $mailClient ; public function init( ) { // ʮAmazon Simple Email ServiceʯΛ࢖༻ͯ͠ϝʔϧΛѻ͏Ϋϥε $this->mailClient = new AwsSes() // EmailSenderΫϥε͕AwsSesΫϥεʹґଘ͍ͯ͠Δ } public function sendRegisterMail($address, $text ) { $this->mailClient->send($address, $text) ; } }

Slide 28

Slide 28 text

&NBJM4FOEFS͕࣋ͭ՝୊ w ಛఆͷαʔϏεʢ͜ͷ৔߹͸"NB[PO4&4ʣʹґଘ͍ͯ͠Δɻ w αʔϏεΛม͑Δඞཁ͕ग़͖ͯͨΒɺ&NBJM4FOEFS಺෦ʹखΛೖΕ ͳͯ͘͸͍͚ͳ͍ɻ w &NBJM4FOEFSΫϥεͷςετʹ"XT4FTΫϥε΋ඞཁʹͳΔɻ w ςετͮ͠Β͍ঢ়ଶʹͳΔɻ

Slide 29

Slide 29 text

ͯ͞ɺͲ͏͢Δ͔ʁ DIͷ֓࿦

Slide 30

Slide 30 text

ྫɿ&NBJMΛૹ৴͢ΔΫϥεɾվ class EmailSender { private $mailClient ; public function __construct($client ) { $this->mailClient = $client; // αʔϏεΛѻ͏ΫϥεΛ֎෦͔Βࢦఆ͢ΔΑ͏ʹมߋ } public function sendRegisterMail($address, $text ) { $this->mailClient->send($address, $text) ; } } &NBJM4FOEFSͰ࢖༻͢ΔϝιουΛ JOUFSGBDFΛ࢖༻ͯ͠දݱ

Slide 31

Slide 31 text

&NBJMΛૹ৴͢ΔΠϯλϑΣʔεྫ interface iEmailClient { public function send($address, $text) ; } class AwsSes implements iEmailClient { private $mailClient ; public function __construct( ) { $this->mailClient = new AwsSes() ; } public function send($address, $text){ } } &NBJM4FOEFS͕ґଘ͢Δ෦෼Λ JOUFSGBDFͱͯ͠දݱ͠ɺ ґଘ౓ͷ௿ݮΛਤ͍ͬͯΔ

Slide 32

Slide 32 text

&NBJM4FOEFSΫϥεͷ%* w Ϋϥεࣗ਎͕ґଘઌΛܾΊΔͷͰ͸ͳ͘ɺΫϥεΛѻ͏ଆ͕ґଘઌΛ ܾఆ͍ͯ͠Δɻ w ͜Ε͕ɺґଘؔ܎ʹର͢Δ੍ޚͷٯసɻ w ςετʹඞཁͳϞοΫ͕࡞Γ΍͘͢ͳΓɺςελϏϦςΟ΋޲্ͯ͠ ͍Δɻ

Slide 33

Slide 33 text

͏ͬ͢Βͱཧղ͕ਐΜͩͱ͜ΖͰ ࿩୊Λগ͠ม͑·͢ DIͷ֓࿦

Slide 34

Slide 34 text

%*ʹର͢Δޡղ

Slide 35

Slide 35 text

%*Λௐ΂Α͏ͱͯ͠ݕࡧ͚ͨ͠Ͳɺ ݁ہΑ͘Θ͔Βͳ͔ͬͨɻ DIʹର͢Δޡղ

Slide 36

Slide 36 text

͜Μͳܦݧ͸͋Γ·ͤΜͰ͔ͨ͠ʁ DIʹର͢Δޡղ

Slide 37

Slide 37 text

%*ʹର͢Δޡղ w %*͸ϢχοτςετΛࢧԉ͢Δͷ͕໨తͰ͋Δɻ w %*ʹ͸%*$POUBJOFSΛ࢖͏ඞཁ͕͋Δɻ w %*͸4FSWJDF-PDBUPSύλʔϯͰ͋Δɻ ޡղ͠΍͍͢ঢ়گ͕ଟ͍

Slide 38

Slide 38 text

̍ͭͣͭݟ͍͖ͯ·͠ΐ͏ DIʹର͢Δޡղ

Slide 39

Slide 39 text

ςετΛࢧԉ͢Δͷ͕໨తͰ͸ͳ͍ w ςελϏϦςΟͷ޲্͸ͭͷଆ໘Ͱ͔͠ͳ͍ɻʮ໨తʯͰ͸ͳ͘ɺ ʮ݁ՌʯͰ͔͠ͳ͍ɻ w %*͸ʮૄ݁߹ͷ࣮ݱΛ໨తͱͨ͠σβΠϯύλʔϯʯͰ͋Δͱɺεϥ Πυͷ๯಄Ͱ΋ड़΂·ͨ͠ɻ w Ϋϥε΍Ϟδϡʔϧؒͷૄ݁߹Λ࣮ݱ͠ɺอकੑͷߴ͍ίʔυΛ࡞Δ ͜ͱ͕ςελϏϦςΟͷ޲্ʹܨ͕Δɻ

Slide 40

Slide 40 text

%*$POUBJOFS͸ඞਢͰ͸ͳ͍ w ϑϨʔϜϫʔΫଆ͕%*ίϯςφΛ࢖͏લఏͰ࢓૊ΈΛఏڙ͍ͯͯ͠ɺ ։ൃऀଆ͕ޡղͯ͠͠·͏ɻ w ޡͬͨղઆهࣄ΍4UBDL0WFS fl PXΛͨ·ͨ·ݟ͚ͭͯ͠·͏ɻ

Slide 41

Slide 41 text

%*$POUBJOFSʹ͍ͭͯ গ͚ͩ͠৮Ε͓͖ͯ·͢ DIʹର͢Δޡղ

Slide 42

Slide 42 text

%*$POUBJOFSͷ؆୯ͳઆ໌ w %*ύλʔϯΛαϙʔτ͢ΔϥΠϒϥϦͰ͢ɻ w ΦϒδΣΫτͷॳظԽ΍߹੒ɺϥΠϑλΠϜͷ؅ཧͱ͍ͬͨ໾ׂΛ ୲͍·͢ɻ w ґଘؔ܎Λఆ͓ٛͯ͘͜͠ͱͰɺΦϒδΣΫτಉ࢜ͷґଘղܾΛ $POUBJOFSଆʹ೚ͤΔ͜ͱ͕Ͱ͖Δɻ w ༷ʑͳݴޠʢ$ɺ+BWBɺ1)1ɺ(P౳ʣͰ։ൃ͕͞Ε͍ͯΔɻ

Slide 43

Slide 43 text

4FSWJDF-PDBUPSύλʔϯͰ͸ͳ͍ w ͍ͣΕ΋Ϋϥεಉ࢜Λૄ݁߹ʹอͭ͜ͱΛ໨తͱͨ͠σβΠϯύλʔ ϯɻ໨త͸ಉ͕ͩ͡ɺґଘؔ܎ͷղܾख๏͕ҟͳΔɻ w %*͕4FSWJDF-PDBUPSͷҰ෦෼ͱ͍͏࿩Ͱ΋ͳ͍Ͱ͢ɻ w ࠞಉ͠΍͍͢ͷͰɺࠓճͷεϥΠυͷ࢒Γ͸%*ͱ4FSWJDF-PDBUPSύ λʔϯͷൺֱʹׂ͍͍ͯ·͢ɻ

Slide 44

Slide 44 text

4FSWJDF-PDBUPSʹ͍ͭͯ

Slide 45

Slide 45 text

4FSWJDF-PDBUPSͱ͸ ґଘઌΛղܾ͢Δ໾ׂΛ࣋ͬͨ ΦϒδΣΫτ ServiceLocatorʹ͍ͭͯ

Slide 46

Slide 46 text

લड़ͨ͠&NBJM4FOEFS Λݩʹͨ͠ਤͰݟͯΈ·͠ΐ͏ ServiceLocatorʹ͍ͭͯ

Slide 47

Slide 47 text

%*ύλʔϯͰදݱͨ͠Ϋϥεਤ

Slide 48

Slide 48 text

4FSWJDF-PDBUPSͰ දݱͨ͠Ϋϥεਤ w 4FSWJDF-PDBUPS͕௥Ճ ͞Ε͓ͯΓɺґଘઌͷ ղܾΛ୲౰͢Δɻ

Slide 49

Slide 49 text

%*ύλʔϯͱ4FSWJDF-PDBUPSύλʔϯ w %*ύλʔϯ͸ɺ&NBJM4FOEFSΫϥεͷґଘઌΛར༻ݩͰ͋Δ "QQMJDBUJPO͕ܾΊ͍ͯΔɻ w 4FSWJDF-PDBUPS͸ɺ&NBJM4FOEFSΫϥεͷґଘઌΛ4FSWJDF-PDBUPS ͕ܾΊ͍ͯΔɻ w &NBJM4FOEFSͱ"XT$MJFOUΛૄ݁߹ʹ͢Δ໨త͸ͲͪΒͷख๏Ͱ΋࣮ ݱͰ͖Δɻ

Slide 50

Slide 50 text

ൺֱͨ͠ॴײ w %*ύλʔϯͷํ͕࢖͍΍͍͢ͱࢥ͑·͢ɻ w 4FSWJDF-PDBUPS΁ͷґଘ͕ൃੜ͍ͯ͠ΔͷͰɺςετ͢ΔͨΊʹ 4FSWJDF-PDBUPSͷ࠶ݱʢϞοΫԽʣ͕ඞཁʹͳΔɻ w ґଘؔ܎ͷղܾΛҕͶΔͱ͍͏఺ʹஔ͍ͯ͸ɺ4FSWJDF-PDBUPSͱ%* $POUBJOFS͕ࣅͨ΋ͷʹࢥ͑Δɻ w ˞ࣗ෼ͷதͰݴޠԽ͖͠Εͯͳ͍ͷͰɺ͜͜͸ҙݟΛٻΊ͍ͨͱ͜Ζɻ

Slide 51

Slide 51 text

ຊ೔ͷ·ͱΊ

Slide 52

Slide 52 text

ຊ೔ͷ·ͱΊ w %*͸ૄ݁߹ͷ࣮ݱΛ໨తͱͨ͠σβΠϯύλʔϯɻ w *OWFSTJPOPG$POUSPMͷͭͷଆ໘ΛσβΠϯύλʔϯͱ໋໊ͯ͠͠ ͨͷ͕ɺʮ%*%FQFOEFODZ*OKFDUJPOʯɻ w %*$POUBJOFS͸%*ύλʔϯΛαϙʔτ͢ΔϥΠϒϥϦɻ w 4FSWJDF-PDBUPS͸%*ͱ໨త͸ಉ͕ͩ͡ɺख๏͕ҟͳΔσβΠϯύ λʔϯɻ

Slide 53

Slide 53 text

ࢀߟจݙ • ʮDependency Injection Principles, Practices, and Patternsʯ • https://www.amazon.com/dp/161729473X • ※ O’Reilly online learningͰಡΊ·͢ɻ • https://martinfowler.com/bliki/InversionOfControl.html • https://martinfowler.com/articles/injection.html

Slide 54

Slide 54 text

%FQFOEFODZ*OKFDUJPO Λཧղ͢Δॿ͚ʹͳΕ͹޾͍Ͱ͢ɻ

Slide 55

Slide 55 text

ൃදʹର͢ΔϑΟʔυόοΫ͸ େ׻ܴͰ͢ʂ

Slide 56

Slide 56 text

͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ʂ