2017/03 版 DDDパターンを活用した Laravelアプリケーション開発/201703-ddd-with-laravel

2017/03 版 DDDパターンを活用した Laravelアプリケーション開発/201703-ddd-with-laravel

Ca17a082a30f4cbfed1d0a6dacbe3af2?s=128

shin1x1

March 23, 2017
Tweet

Transcript

  1. ɹ!shin1x1 2017//23 <൛> %%%ύλʔϯΛ׆༻ͨ͠ -BSBWFMΞϓϦέʔγϣϯ։ൃ

  2. https://github.com/shin1x1/laravel-ddd-sample

  3. D .BTBTIJ4IJOCBSB!TIJOY ΞδΣϯμ wର৅Ϣʔεέʔε w%%% wύλʔϯΛ׆༻࣮ͨ͠૷ w࣮૷ͷϙΠϯτ

  4. ର৅γεςϜ
 Ϣʔεέʔε

  5. &$αΠτ ΧʔτʹೖΕΔ

  6. D .BTBTIJ4IJOCBSB!TIJOY ΧʔτʹೖΕΔ"1*  1045ϦΫΤετ͔Β঎඼*%ͱ঎඼ݸ਺Λऔಘ  σʔλϕʔε͔Β঎඼*%Ͱ঎඼Λऔಘ  Χʔτʹ঎඼ΛೖΕΔ 

    ΧʔτΛηογϣϯʹอଘ  ϨεϙϯεΛฦ͢
  7. %PNBJO %SJWFO %FTJHO

  8. D .BTBTIJ4IJOCBSB!TIJOY %PNBJO%SJWFO%FTJHO wʮΤϦοΫɾΤϰΝϯεͷυϝΠϯۦಈઃܭʯ wυϝΠϯʹϑΥʔΧεͨ͠ઃܭɺ։ൃख๏ wଟ͘ͷύλʔϯ͕঺հ͞Ε͍ͯΔ

  9. D .BTBTIJ4IJOCBSB!TIJOY ιϑτ΢ΣΞͷυϝΠϯ wγεςϜͷର৅ྖҬɺղܾ͢Δ໰୊ྖҬ wۀ຿γεςϜͰ͋Ε͹ɺ
 ର৅ͷϏδωε͕υϝΠϯ wιʔείʔυ؅ཧγεςϜͰ͋Ε͹ɺ
 ιϑτ΢ΣΞ։ൃ͕υϝΠϯ wຊηογϣϯͰ͸ɺ&$αΠτ͕υϝΠϯ

  10. ϢϏΩλεݴޠ D .BTBTIJ4IJOCBSB!TIJOY wυϝΠϯ༻ޠࣙॻ
 ঎඼ɺΧʔτɺΧʔτʹ঎඼ΛೖΕΔɺͳͲ wϓϩδΣΫτνʔϜ಺Ͱಉ͡ݴ༿Λར༻
 ։ൃऀ΋ϏδωεΤΩεύʔτ΋શһ wͦͷݴ༿ΛίʔυͰ΋ར༻
 ʢ໊લۭؒɺΫϥεɺϝιου౳ʣ

  11. ӳޠ ಺༰ ঎඼ *UFN ൢച͢Δ঎඼ɻԼهͷଐੑΛ࣋ͭɻ
 ɾ঎඼*% ɾ঎඼໊
 ɾ঎඼Ձ֨
 ɾࡏݿ ঎඼*%

    *UFN*E ঎඼Λࣝผ͢Δίʔυ
 ঎඼͝ͱʹҰҙ ঎඼໊ *UFN/BNF ঎඼ͷ໊শ ঎඼Ձ֨ *UFN1SJDF ঎඼ͷൢചՁ֨
 Ҏ্ͷ੔਺ ࡏݿ *UFN4UPDL ঎඼ͷࡏݿ਺ Ҏ্ͷ੔਺
  12. ӳޠ ಺༰ Χʔτ $BSU ߪೖ͢Δ঎඼ΛҰ࣌֨ೲ͢ΔೖΕ෺
 ΧʔτཁૉΛෳ਺࣋ͭ Χʔτཁૉ $BSU&MFNFOU Χʔτʹ֨ೲ͞Ε͍ͯΔཁૉ
 ঎඼໊ͱ঎඼ݸ਺Λଐੑʹ࣋ͭ

    ঎඼ݸ਺ *UFN$PVOU ঎඼ͷݸ਺ɻҎ্ͷ੔਺ɻ Χʔτʹ঎඼Λ ೖΕΔ "EE*UFN5P$BSU Χʔτʹ঎඼ΛೖΕΔ͜ͱɻ
 ঎඼ͱ঎඼ݸ਺Λ༩͑Δɻ
 ঎඼ݸ਺͸঎඼ࡏݿҎԼͰ͋Δඞཁ ͕͋Δɻ
  13. ϨΠϠʔυΞʔΩςΫνϟ D .BTBTIJ4IJOCBSB!TIJOY wγεςϜΛϨΠϠʢ૚ʣʹ෼ׂ wϨΠϠ͸ࣗ਎ͷ໾ׂΛ୲͏ wϨΠϠؒͰڠௐͯ͠ɺॲཧΛߦ͏ wϨΠϠؒͷྲྀΕ͸Ұํ௨ߦ

  14. %%%&SJD&WBOT D .BTBTIJ4IJOCBSB!TIJOY UI Application Infrastructure Domain

  15. ϨΠϠؒͷྲྀΕ D .BTBTIJ4IJOCBSB!TIJOY UI Application Infrastructure Domain

  16. D .BTBTIJ4IJOCBSB!TIJOY 6*-BZFS wϢʔβʢ࿈ܞγεςϜؚΉʣͱγεςϜͷ઀఺ wϢʔβ͸͜ͷϨΠϠΛ௨ͯ͡γεςϜʹ৮ΕΔ w)551 )5.- +40/ 45%*/ 45%065

    wϏϡʔɺ3FRVFTUɺ3FTQPOTF
  17. D .BTBTIJ4IJOCBSB!TIJOY "QQMJDBUJPO-BZFS wγεςϜͷίϯτϩʔϧΛ୲͏ϨΠϠ wϢʔεέʔεͷྲྀΕΛ࡞Γɺ۩ମతͳॲཧ͸
 υϝΠϯ΍ΠϯϑϥετϥΫνϟʹҕৡ w3PVUF$POUSPMMFS6TF$BTF

  18. D .BTBTIJ4IJOCBSB!TIJOY %PNBJO-BZFS wγεςϜͷυϝΠϯΛ࣮૷͢ΔϨΠϠ wυϝΠϯҎ֎ͷؔ৺ࣄ͸ؚΊͳ͍
 )551%#Ωϟογϡ౳ w1010Ͱ࣮૷͠ɺϑϨʔϜϫʔΫ͔Β͸ಠཱ
 1MBJO0ME1)10CKFDU wϞσϧɺαʔϏεɺΠϕϯτͳͲ

  19. D .BTBTIJ4IJOCBSB!TIJOY *OGSBTUSVDUVSF-BZFS wσʔλετΞ΍ϝʔϧૹ৴ɺ֎෦"1*࿈ܞͳͲ
 Ұൠతͳٕज़ػೳΛఏڙ͢ΔϨΠϠ wϦϙδτϦɺΩϟογϡɺ"1*ΫϥΠΞϯτͳͲ w&MPRVFOU2VFSZ#VJMEFS$BDIF

  20. ඞཁ࠷௿ݶ͚ͩ ར༻ ͪ͜Βʹ࣮૷

  21. ΠϯϑϥετϥΫνϟ ΞϓϦέʔγϣϯ υϝΠϯ

  22. ύλʔϯΛ׆༻࣮ͨ͠૷

  23. D .BTBTIJ4IJOCBSB!TIJOY UI Application Infrastructure Domain %PNBJO-BZFS

  24. D .BTBTIJ4IJOCBSB!TIJOY ΧʔτʹೖΕΔ"1*  1045ϦΫΤετ͔Β঎඼*%ͱ঎඼ݸ਺Λऔಘ  σʔλϕʔε͔Β঎඼*%Ͱ঎඼Λऔಘ  Χʔτʹ঎඼ΛೖΕΔ 

    ΧʔτΛηογϣϯʹอଘ  ϨεϙϯεΛฦ͢
  25. D .BTBTIJ4IJOCBSB!TIJOY ΤϯςΟςΟ wυϝΠϯϞσϧͷ࣮૷
 ঎඼ɺΧʔτ w&MPRVFOUΛܧঝ͠ͳ͍ wಉҰੑ͸ࣝผࢠʢJEʣͰ൑ผ wϛϡʔλϒϧ

  26. D .BTBTIJ4IJOCBSB!TIJOY ঎඼ΤϯςΟςΟ ঎඼ΤϯςΟςΟ͸ҎԼͷଐੑΛ࣋ͭ ଐੑ ಺༰ ৼΔ෣͍ʢԋࢉʣ ঎඼*% ঎඼ͷࣝผࢠ ঎඼͝ͱʹҰҙ

    ͳ͠ ঎඼໊ ঎඼ͷ໊শ ͳ͠ ঎඼Ձ֨ ঎඼ͷՁ֨ Ҏ্ͷ੔਺ ɾ঎඼ݸ਺ͱͷֻ͚ࢉ ࡏݿ ঎඼ͷࡏݿ਺ Ҏ্ͷ੔਺ ɾࡏݿ͕ෆ଍͍ͯ͠Δ͔
  27. &MPRVFOU 3FQPTJUPSZ class Item
 {
 /** @var int */
 private

    $id;
 
 /** @var string */
 private $name;
 
 /** @var int */
 private $price;
 
 /** @var int */
 private $stock;
 }
 int int int *UFNʢ঎඼ʣ
  28. &MPRVFOU 3FQPTJUPSZ class Item
 {
 /** @var int */
 private

    $id;
 
 /** @var string */
 private $name;
 
 /** @var int */
 private $price;
 
 /** @var int */
 private $stock;
 }
 int int int *UFNʢ঎඼ʣ wଐੑͷ಺༰͕൓ө͞Ε͍ͯͳ͍ wUIJTQSJDF
 ૝ఆ֎ͷ࢖͍ํʢ஋Ҿ͖༻঎඼ʣ wUIJTTUPDL UIJTTUPDL
 ͋Γ͑ͳ͍ԋࢉ
  29. D .BTBTIJ4IJOCBSB!TIJOY 7BMVF0CKFDU wՁ֨΍೔෇ͳͲϓϦϛςΟϒͳ஋Λࣔ͢
 ঎඼*%ɺ঎඼Ձ֨ɺࡏݿ wಉҰੑ͸ଐੑͰ൑ผ w஋ʹର͢Δ੍໿΍ৼΔ෣͍ʢԋࢉʣΛ࣮૷ wΠϛϡʔλϒϧ

  30. &MPRVFOU 3FQPTJUPSZ class Item
 {
 /** @var ItemId */
 private

    $id;
 
 /** @var string */
 private $name;
 
 /** @var ItemPrice */
 private $price;
 
 /** @var Stock */
 private $stock;
 }
 ItemId ItemPrice Stock ܕʢΫϥεʣͰදݱ
  31. &MPRVFOU 3FQPTJUPSZ class ItemId
 {
 /** @var int */
 private

    $value;
 
 /**
 * @param int $value
 */
 public function __construct(int $value)
 {
 $this->value = $value;
 }
 
 public function value(): int
 {
 return $this->value;
 }
 } *UFN*E ৼΔ෣͍ʢԋࢉʣ͕ແ͍
  32. &MPRVFOU 3FQPTJUPSZ class ItemPrice
 {
 /** @var int */
 private

    $value;
 
 
 public function __construct(int $value = 0)
 {
 if ($value < 0) {
 throw new InvariantException('value must be positive number:' . $value);
 }
 $this->value = $value;
 }
 //(snip)
 
 *UFN1SJDF ঎඼Ձ֨͸Ҏ্ͷ੔਺
  33. &MPRVFOU 3FQPTJUPSZ 
 public function calcSubtotal(ItemCount $count): ItemSubtotal
 {
 return

    new ItemSubtotal($this->value * $count->value());
 }
 *UFN1SJDF ঎඼Ձ֨ ঎඼ݸ਺঎඼খܭ
  34. D .BTBTIJ4IJOCBSB!TIJOY 7BMVF0CKFDUͷϙΠϯτ wϢϏΩλεݴޠΛίʔυͰදݱ
 ίʔυΛݟΕ͹࢓༷͕෼͔Δ wϝιουͰఆٛͨ͠ԋࢉ͔͠Ͱ͖ͳ͍
 ؒҧ͍ΛݮΒͤΔ w൚༻ܕͰ͸ͳ͘ಛԽܕͰ
 ʮ਺ࣈʯͰ͸ͳ͘ʮ঎඼Ձ֨ʯ wڽू౓্͕͕Γɺѻ͍΍͍͢ɻςετ͕༰қʹɻ

  35. &MPRVFOU 3FQPTJUPSZ class Cart
 {
 /** @var CartElement[]|Collection */
 private

    $elements;
 
 public function addItem(Item $item, ItemCount $count)
 {
 if (!$item->isSufficient($count)) {
 throw new InvariantException('stock is insufficient');
 }
 
 $this->elements->push(new CartElement($item, $count));
 }
 $BSU ࡏݿ͕଍Γͳ͚Ε͹ྫ֎
  36. &MPRVFOU 3FQPTJUPSZ class Cart
 {
 /** @var CartElement[]|Collection */
 private

    $elements;
 
 public function addItem(Item $item, ItemCount $count)
 {
 if (!$item->isSufficient($count)) {
 throw new PreconditionsException('stock is insufficient');
 }
 
 $this->elements->push(new CartElement($item, $count));
 }
 $BSU ΧʔτཁૉΛ௥Ճ
  37. D .BTBTIJ4IJOCBSB!TIJOY υϝΠϯϨΠϠͷ࣮૷ wϑϨʔϜϫʔΫ͔Βಠཱͯ͠1010Ͱ࣮૷
 $PMMFDUJPOͳͲυϝΠϯΛදݱ͢Δ΋ͷ͸PL wଞϨΠϠʹ͸ґଘ͠ͳ͍ w7BMVF0CKFDUͰυϝΠϯಛԽܕΛ࡞Γɺ
 ϢϏΩλεݴޠΛදݱ wυϝΠϯϨΠϠͰ͸70Λ࠷খཻ౓ʹ͢Δ
 70಺Ҏ֎Ͱ͸εΧϥʔܕ͸࢖Θͳ͍ʢཧ૝ʣ

  38. D .BTBTIJ4IJOCBSB!TIJOY UI Application Infrastructure Domain *OGSBTUSVDUVSF-BZFS

  39. D .BTBTIJ4IJOCBSB!TIJOY ΧʔτʹೖΕΔ"1*  1045ϦΫΤετ͔Β঎඼*%ͱ঎඼ݸ਺Λऔಘ  σʔλϕʔε͔Β঎඼*%Ͱ঎඼Λऔಘ  Χʔτʹ঎඼ΛೖΕΔ 

    ΧʔτΛηογϣϯʹอଘ  ϨεϙϯεΛฦ͢
  40. D .BTBTIJ4IJOCBSB!TIJOY ϦϙδτϦ wϞσϧͱσʔλετΞͷؒʹհࡏ͠ɺ෼཭ wσʔλετΞ͔ΒͷϞσϧͷऔಘɺอଘϝιου
 ΛΠϯλʔϑΣΠεͰఆٛ wσʔλετΞʹԠͯ͡ɺΠϯλʔϑΣΠεΛ࣮૷
 σʔλϕʔεɺ,74ɺϝϞϦͳͲ

  41. D .BTBTIJ4IJOCBSB!TIJOY ϦϙδτϦΠϯλʔϑΣʔε wσʔλετΞʢ3%#.4౳ʣ͔Βɹ
 *UFNϞσϧͷೖग़ྗΛ୲͏ wυϝΠϯϨΠϠʹΠϯλʔϑΣʔεͱͯ͠ఆٛ

  42. $POUSPMMFS %BUB5SBOTGFS0CKFDU υϝΠϯϨΠϠ ϦϙδτϦ ΠϯλʔϑΣΠε

  43. &MPRVFOU 3FQPTJUPSZ interface ItemRepository
 {
 public function findById(ItemId $id): Item;


    } *UFN3FQPTJUPSZ
  44. D .BTBTIJ4IJOCBSB!TIJOY ϦϙδτϦͷ࣮૷ wσʔλϕʔεJUFNςʔϒϧͱͷೖग़ྗ w&MPRVFOUͰσʔλϕʔεૢ࡞Λߦ͏ w*UFN3FQPTJUPSZΛ࣮૷

  45. None
  46. ΠϯϑϥετϥΫνϟ $POUSPMMFS %BUB5SBOTGFS0CKFDU 3FQPTJUPSZ*OUFSGBDF 'PSN3FRVFTU "QQMJDBUJPO4FSWJDF ΠϯϑϥετϥΫνϟϨΠϠ ϦϙδτϦ &MPRVFOU

  47. &MPRVFOU 3FQPTJUPSZ class EloquentItemRepository implements ItemRepository
 {
 private $eloquent;
 


    public function __construct(EloquentItem $eloquent)
 {
 $this->eloquent = $eloquent;
 }
 
 public function findById(ItemId $id): Item
 {
 $item = $this->eloquent->find($id->value());
 if (empty($item)) {
 throw new NotFoundException('item not found');
 }
 
 return $item->toDomain();
 }
 } &MPRVFOU*UFN3FQPTJUPSZ
  48. &MPRVFOU 3FQPTJUPSZ class EloquentItemRepository implements ItemRepository
 {
 private $eloquent;
 


    public function __construct(EloquentItem $eloquent)
 {
 $this->eloquent = $eloquent;
 }
 
 public function findById(ItemId $id): Item
 {
 $item = $this->eloquent->find($id->value());
 if (empty($item)) {
 throw new NotFoundException('item not found');
 }
 
 return $item->toDomain();
 }
 } &MPRVFOU*UFN3FQPTJUPSZ
  49. &MPRVFOU 3FQPTJUPSZ class EloquentItem extends AppEloquent implements Domainable
 {
 protected

    $table = 'items';
 
 public function toDomain(): Item
 {
 return new Item(
 new ItemId($this->id),
 $this->name,
 new ItemPrice($this->price),
 new Stock($this->stock)
 );
 }
 } &MPRVFOU*UFN
  50. &MPRVFOU 3FQPTJUPSZ class RepositoryServiceProvider extends ServiceProvider
 {
 public function register()


    {
 $this->app->bind(ItemRepository::class, EloquentItemRepository::class);
 }
 } ϦϙδτϦΛαʔϏείϯςφ΁όΠϯυ
  51. &MPRVFOU 3FQPTJUPSZ class AddItemToCart
 {
 private $itemRepo;
 
 public function

    __construct(ItemRepository $itemRepo)
 {
 $this->itemRepo = $itemRepo;
 }
 
 public function __invoke(int $itemId, int $count): Cart
 {
 $item = $this->itemRepo->findById(new ItemId($itemId));
 // (snip)
 }
 }
 ར༻ίʔυ
  52. D .BTBTIJ4IJOCBSB!TIJOY &MPRVFOU w&MPRVFOUʹυϝΠϯϩδοΫ͸࣮૷͠ͳ͍ w&MPRVFOUΛ1FSTJTUFODF.PEFMʹ࢖͏ wυϝΠϯϨΠϠͰ&MPRVFOU͸࢖Θͳ͍

  53. &MPRVFOU 3FQPTJUPSZ public function find(): Cart
 {
 $cart = $this->session->get(static::SESSION_KEY);


    if (empty($cart)) {
 throw new NotFoundException('cart not found');
 }
 
 return $cart;
 }
 
 public function store(Cart $cart)
 {
 $this->session->put(static::SESSION_KEY, $cart);
 }
 )UUQ4FTTJPO$BSU3FQPTJUPSZ ηογϣϯ͔Βͷೖग़ྗ
  54. D .BTBTIJ4IJOCBSB!TIJOY ϦϙδτϦͷϝϦοτ wυϝΠϯϨΠϠ͔ΒσʔλετΞ΁ͷ࣮૷Λഉআ
 υϝΠϯϨΠϠ͸ΠϯλʔϑΣΠεͷϝιουΛݺͿ͚ͩ
 Ͳ͜ʹετΞ͞ΕΔ͔͸υϝΠϯϨΠϠ͸஌Βͳ͍ wσʔλετΞ΍ΞΫηεखஈͷ੾ସ͕༰қ
 ຊ൪͸1PTUHSF42-ςετ͸ϝϞϦ wσʔλετΞ΁ͷඞཁͳૢ࡞Λ໌ࣔ
 7BMVF0CKFDUͱಉ༷

  55. D .BTBTIJ4IJOCBSB!TIJOY UI Application Infrastructure Domain "QQMJDBUJPO-BZFS

  56. D .BTBTIJ4IJOCBSB!TIJOY ΧʔτʹೖΕΔ"1*  1045ϦΫΤετ͔Β঎඼*%ͱ঎඼ݸ਺Λऔಘ  σʔλϕʔε͔Β঎඼*%Ͱ঎඼Λऔಘ  Χʔτʹ঎඼ΛೖΕΔ 

    ΧʔτΛηογϣϯʹอଘ  ϨεϙϯεΛฦ͢
  57. &MPRVFOU 3FQPTJUPSZ public function post(Request $request, AddItemToCart $useCase): JsonResponse
 {


    $this->validate($request, [
 'item_id' => 'required|integer',
 'count' => 'required|integer'
 ]);
 
 $cart = $useCase(
 $request->get('item_id'),
 $request->get('count')
 );
 return response()->json(['cart' => $this->cartToArray($cart)]); } $POUSPMMFS ͬ͘͟ΓόϦσʔγϣϯ
 ʢυϝΠϯϧʔϧ͸υϝΠϯϨΠϠͰʣ
  58. &MPRVFOU 3FQPTJUPSZ $POUSPMMFS public function post(Request $request, AddItemToCart $useCase): JsonResponse


    {
 $this->validate($request, [
 'item_id' => 'required|integer',
 'count' => 'required|integer'
 ]);
 
 $cart = $useCase(
 $request->get('item_id'),
 $request->get('count')
 );
 return response()->json(['cart' => $this->cartToArray($cart)]); } ϢʔεέʔεΛ࣮ߦ
  59. &MPRVFOU 3FQPTJUPSZ public function _invoke(int $itemId, int $count): Cart
 {


    $item = $this->itemRepo->findById(new ItemId($itemId));
 
 try {
 $cart = $this->cartRepo->find();
 } catch (NotFoundException $e) {
 $cart = new Cart();
 }
 
 $cart->addItem($item, new ItemCount($count));
 $this->cartRepo->store($cart);
 
 return $cart;
 }
 6TF$BTF
  60. ࣮૷ͷϙΠϯτ

  61. D .BTBTIJ4IJOCBSB!TIJOY ෼ׂ͢Δ wΞϓϦέʔγϣϯͱϑϨʔϜϫʔΫͷ෼཭ wυϝΠϯϨΠϠͱͦΕҎ֎ͷ෼཭ wϞσϧͷଐੑΛ7BMVF0CKFDU΁෼཭

  62. D .BTBTIJ4IJOCBSB!TIJOY ಛԽ͢Δ wυϝΠϯʹಛԽɺίϯςΩετʹಛԽ
 ڥք෇͚ΒΕͨίϯςΩετ wυϝΠϯ΍ίϯςΩετΛ௒͑ͨ
 ൚༻Λ໨ࢦ͠ա͗ͳ͍ wίϯςΩετ಺Ͱͷ൚༻͸PL

  63. D .BTBTIJ4IJOCBSB!TIJOY ओैΛ͓͑͞Δ wΞϓϦέʔγϣϯ͕ओɺϑϨʔϜϫʔΫ͸ै wυϝΠϯϨΠϠ͕ओɺΞϓϦέʔγϣϯ͸ै

  64. D .BTBTIJ4IJOCBSB!TIJOY ͍͞͝ʹ ࡏݿͬͯɺ঎඼͕ѻ͏΂͖ͳͷ͔ͳʁ

  65. D .BTBTIJ4IJOCBSB!TIJOY ͍͞͝ʹ ࡏݿνΣοΫ͸ɺΧʔτͷதͰ΍Δ͜ͱʁ

  66. D .BTBTIJ4IJOCBSB!TIJOY ͍͞͝ʹ υϝΠϯʹண໨͢Δͱ ͜Μͳٙ໰͕૿͖͑ͯ·͢ɻ ͦͷࢹ఺Λ࣋ͭ͜ͱ͕υϝΠϯΛΑΓཧղ͠ɺ ΞϓϦέʔγϣϯʹ൓ө͢ΔୈҰาͰ͢ɻ

  67. D .BTBTIJ4IJOCBSB!TIJOY !TIJOY !TIJOY D .BTBTIJ4IJOCBSB!TIJOY