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

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

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

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

Kazuhei Arai

May 08, 2019
Tweet

More Decks by Kazuhei Arai

Other Decks in Programming

Transcript

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

    & A ʹ ྲྀ ௨ ֵ ໋ Λ
  2. αʔϏείϯςφͱ͸ <?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); } } Πϯελϯεͷొ࿥
  3. αʔϏείϯςφͱ͸ <?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()]); } } Πϯελϯεͷར༻
  4. σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ <?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
  5. σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ <?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
  6. σʔλΞΫηεʹґଘ͠ͳ͍υϝΠϯΫϥεΛ࡞Δ <?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
  7. *OUFSGBDFΛ׆༻ <?php namespace Domain\User; interface UserRepositoryInterface { public function get(UserId

    $id): User; public function getList(): UserList; } 3FQPTJUPSZ*OUFSGBDF
  8. *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
  9. %#ΞΫηεΛࠩ͠ସ͑Δ 3FQPTJUPSZ *OUFSGBDF %PNBJO *OGSB &MPRVFOU 3FQPTJUPSZ *NQMFNFOU .PEFM &MPRVFOU

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

    $BDIFE 3FQPTJUPSZ *NQMFNFOU .PEFM &MPRVFOU $BDIF ܧঝ $BDIFΛ࢖͍ͬͯΔ͔Ͳ͏͔Λؾʹ͠ͳ͍ %PNBJO