サービスコンテナの実践的な活用

 サービスコンテナの実践的な活用

【シューマイ】Tech Lead Engineerから最新技術を学べ!Laravel編

9184d05c3493636df20a38b8bcfd66df?s=128

Kazuhei Arai

May 08, 2019
Tweet

Transcript

  1. !LB[VIFJ !LB[VIFJ@@ αʔϏείϯςφͷ࣮ફతͳ׆༻ ʲγϡʔϚΠʳ5FDI-FBE&OHJOFFS͔Β࠷৽ٕज़Λֶ΂ʂ-BSBWFMฤ

  2. ࣗݾ঺հ ߥҪ࿨ฏ!LB[VIFJ גࣜձࣾ."Ϋϥ΢υ ീஸງͷͲϕϯνϟʔ

  3. ς Ϋ ϊ ϩ δ ʔ ͷ ྗ Ͱ M

    & A ʹ ྲྀ ௨ ֵ ໋ Λ
  4. ۀքॳʂืूܕ."ϓϥοτϑΥʔϜ

  5. ۀքॳʂืूܕ."ϓϥοτϑΥʔϜ ."ͷϚονϯάϓϥοτϑΥʔϜʮ."Ϋϥ΢υʯ͸ɺ͜ ͷͨͼ೥݄೔ͷαʔϏε։͔࢝Β೥Λܴ͑ɺܝࡌ ങ͍खاۀ਺͸ࣾʢ͏্ͪ৔اۀࣾʣɺొ࿥ചΓखا ۀ਺͸ ࣾΛಥഁ͠·ͨ͠ɻ."Ϋϥ΢υΛ௨ͨ͡ ."੒໿࣮੷ͷྲྀ௨૯ֹ͸ԯ ສԁʹ্͍ͬͯ· ͢ɻ

  6. ໨࣍ w αʔϏείϯςφͱ͸ w αʔϏείϯςφΛੵۃతʹ࢖͏ઃܭ w ࣮ྫ w ·ͱΊ

  7. αʔϏείϯςφͱ͸

  8. αʔϏείϯςφͱ͸ w ҰൠతʹݴΘΕΔ%*ίϯςφʔͷ໾ׂΛ͍࣋ͬͯΔ w %*ίϯςφ͸Πϯελϯεͷੜ੒Λߦͬͯ͘ΕΔ

  9. αʔϏείϯςφͱ͸ <?php namespace App\Providers; use Domain\User\UserRepositoryInterface; use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

    use Infra\User\EloquentUserRepository; class RepositoryServiceProvider extends ServiceProvider { public function boot() { } public function register() { $this->app->bind(UserRepositoryInterface::class, EloquentUserRepository::class); } } Πϯελϯεͷొ࿥
  10. αʔϏείϯςφͱ͸ <?php namespace App\Http\Controllers\User; use Domain\User\UserRepositoryInterface; class IndexController { /**

    * @var UserRepositoryInterface */ private $userRepository; public function __construct(UserRepositoryInterface $userRepository) { $this->userRepository = $userRepository; } public function index() { return view('user.index', ['userList' => $this->userRepository- >getList()]); } } Πϯελϯεͷར༻
  11. αʔϏείϯςφͷར఺ w ੜ੒Λ෼཭Ͱ͖ΔͷͰςετͳͲͷͱ͖ʹศརͩΑͶͱ͍͏ ࿩͕Α͘঺հ͞ΕΔ w ฐࣾͰ͸ςετ࣌Ҏ֎ʹ΋࢖͍ͬͯΔͷͰͦͷ࢖͍ํΛ঺հ ͠·͢

  12. αʔϏείϯςφΛ ੵۃతʹ࢖͏ઃܭ

  13. αʔϏείϯςφΛੵۃతʹ࢖͏ઃܭ  σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ  *OUFSGBDFΛ׆༻

  14. σʔλΞΫηεʹґଘ͠ͳ͍ υϝΠϯΫϥεΛ࡞Δ

  15. σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ .PEFM &MPRVFOU &MPRVFOU͚ͩͷঢ়ଶ

  16. σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ <?php namespace Infra\User; use Illuminate\Foundation\Auth\User as Authenticatable; class User

    extends Authenticatable { public function getDisplayName(): string { return $this->first_name . ' ' . $this->last_name; } } .PEFM
  17. σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ .PEFM &MPRVFOU %PNBJO UP%PNBJO &MPRVFOUΛ%PNBJOΫϥεʹϚοϐϯά͢Δ

  18. σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ <?php namespace Infra\User; use Domain\User\Name; use Domain\User\User; use Domain\User\UserId;

    use Illuminate\Foundation\Auth\User as Authenticatable; class EloquentUserModel extends Authenticatable { public function toDomain(): User { return new User( new UserId($this->id), Name::of($this->first_name, $this->last_name) ); } } .PEFM
  19. σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ <?php namespace Domain\User; class User { /** * @var

    UserId */ private $id; /** * @var Name */ private $name; public function __construct(UserId $id, Name $name) { $this->id = $id; $this->name = $name; } public function getDisplayName(): string { return $this->name->getFullNameString(); } } %PNBJO
  20. σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ w σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞͓ͬͯ͘ w &MPRVFOUʹ௚઀ϩδοΫΛॻ͘ͷ͸΍Ίͯɺ&MPRVFOU͸ %#ૢ࡞ͷͨΊ͚ͩʹ࢖͏

  21. *OUFSGBDFΛ׆༻

  22. *OUFSGBDFΛ׆༻ $POUSPMMFS 7JFX .PEFM &MPRVFOU ݹయతͳ.7$

  23. *OUFSGBDFΛ׆༻ $POUSPMMFS 7JFX *OUFSGBDFΛ࢖ͬͯ૚Λ෼͚Δ 3FQPTJUPSZ *OUFSGBDF %PNBJO "QQ %PNBJO *OGSB

    3FQPTJUPSZ *NQMFNFOU .PEFM &MPRVFOU
  24. *OUFSGBDFΛ׆༻ <?php namespace Domain\User; interface UserRepositoryInterface { public function get(UserId

    $id): User; public function getList(): UserList; } 3FQPTJUPSZ*OUFSGBDF
  25. *OUFSGBDFΛ׆༻ <?php namespace Infra\User; use Domain\User\User; use Domain\User\UserId; use Domain\User\UserList;

    use Domain\User\UserRepositoryInterface; class EloquentUserRepository implements UserRepositoryInterface { public function get(UserId $id): User { $model = EloquentUserModel::where('id', $id->rawValue())->get(); return $model->toDomain(); } public function getList(): UserList { $collection = EloquentUserModel::all(); return new UserList($collection->map(function ($model) { return $model->toDomain(); })->toArray()); } } &MPRVFOU3FQPTJUPSZ*NQMFNFOU
  26. *OUFSGBDFΛ׆༻ w ϨΠϠʔΛ࡞Γ*OUFSGBDFΛ࡞ͬͯσʔλΞΫηε෦෼Λ෼ ཭͢Δ

  27. ࣮ྫ

  28. ࣮ྫ w %#ΞΫηεΛࠩ͠ସ͑Δ w )551ΫϥΠΞϯτΛࠩ͠ସ͑Δ

  29. %#ΞΫηεΛࠩ͠ସ͑Δ

  30. %#ΞΫηεΛࠩ͠ସ͑Δ 3FQPTJUPSZ *OUFSGBDF %PNBJO *OGSB &MPRVFOU 3FQPTJUPSZ *NQMFNFOU .PEFM &MPRVFOU

    2VFSZ#VJMEFS 3FQPTJUPSZ *NQMFNFOU 2VFSZ#VJMEFS ෳ਺ͷ%#ΞΫηεํ๏Λڞଘ %PNBJO
  31. %#ΞΫηεΛࠩ͠ସ͑Δ 3FQPTJUPSZ *OUFSGBDF %PNBJO *OGSB &MPRVFOU 3FQPTJUPSZ *NQMFNFOU .PEFM &MPRVFOU

    $BDIFE 3FQPTJUPSZ *NQMFNFOU .PEFM &MPRVFOU $BDIF ܧঝ $BDIFΛ࢖͍ͬͯΔ͔Ͳ͏͔Λؾʹ͠ͳ͍ %PNBJO
  32. %#ΞΫηεΛࠩ͠ସ͑Δ w ςʔϒϧఆٛͳͲ͕มߋ͞Εͯ΋৽͍͠ 3FQPTJUPSZ*NQMFNFOUΛ࡞Ε͹ରԠͰ͖Δ w $BDIFΞΫηε΋3FQPTJUPSZ*NQMFNFOUΛܧঝ͢Δ͜ͱͰ ରԠͰ͖Δ w ؅ཧπʔϧ͚ͩ$BDIFͳ͠ͱ͔ɺίϯςΩετʹ߹Θ࣮ͤͯ ૷Λબ୒Ͱ͖Δ

    w ։์ด࠯ݪଇ
  33. )551ΫϥΠΞϯτΛ ࠩ͠ସ͑Δ

  34. )551ΫϥΠΞϯτΛࠩ͠ସ͑Δ "QJ$MJFOU *OUFSGBDF %PNBJO *OGSB (V[[MF"QJ$MJFOU
 *NQMFNFOU (V[[MF 4%,"QJ$MJFOU *NQMFNFOU

    -JOF4%, ෳ਺ͷ)551ΞΫηεํ๏Λڞଘ %PNBJO
  35. )551ΫϥΠΞϯτΛࠩ͠ସ͑Δ w (V[[MFͳͲͷ)551ΫϥΠΞϯτ͔ΒαʔϏεͷ4%,ʹࠩ͠ ସ͑ w 4%,ͷόʔδϣϯҧ͍ʹطଘͷΫϥεΛมߋͤͣʹରԠ

  36. ·ͱΊ

  37. ·ͱΊ w αʔϏείϯςφͰΫϥεͷੜ੒Λ෼཭Ͱ͖Δ w σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞͓ͬͯ ͘ͱ༷ʑͳσʔλΞΫηεํ๏Λબ୒Ͱ͖ΔΑ͏ʹͳΔ w ϨΠϠʔΛ࡞Γ*OUFSGBDFΛ࡞ͬͯσʔλΞΫηε෦෼Λ ෼཭͢Δ w

    αʔϏείϯςφͰσʔλΞΫηε༻ͷΫϥεΛࠩ͠ସ ͑ΒΕΔ