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

Laravel × Clean Architecture 新規開発中の現場

193afe5e02597b075f4ba23d40363899?s=47 Ian Brison
December 01, 2019

Laravel × Clean Architecture 新規開発中の現場

「クリーンアーキテクチャ」とは、DDDの文脈における「ドメインモデル」をシステムの中心にすえ、「入力」「永続」「出力」などの多方面で抽象化を行うことで、高度に変化への強さを獲得できるアーキテクチャです。昨今の変化の早さに対応するべく「アジャイル開発」や「マイクロサービス」が叫ばれているように、「クリーンアーキテクチャ」もまた日増しにその注目度は上がっているように感じます。

しかし、やや取っつきにくいアーキテクチャであることからか、現状参考になる資料が無数に転がっているというわけでもありません。そこで、エキテンで現在Laravelベースで開発中の新しい予約システムを題材に、実際にアーキテクチャを形作っているコンポーネントの紹介や、開発する上で行ってみたちょっとした工夫、開発フェーズにおける現場の生の声などをお話しようと思います。

193afe5e02597b075f4ba23d40363899?s=128

Ian Brison

December 01, 2019
Tweet

Transcript

  1. Laravel × Clean Architecture  Twitter: @brison_ian ~  

    ~
  2. LaravelClean Architecture ... 2019/11/30  

  3. &2019/11/30 !"#'   Twitter: @nrslib    Twitter:

    @okashoi 2$ %   
  4.        

  5. $'*!) $% " &# *!   (

  6.    1. )' 2. (!&+-$ *#. 3. ,

     4. %  5.   6. "
  7. 

  8. &  $23% #& &   &  !"&

      Twitter&@brison_ian
  9.   

  10. )! '/ 2- (" DB 1  $0 /DB /.*,3

    (" +,3 /API %DB&* #
  11. ,$ *!260 +% DB 4  '3 !2DB !21-/7 +%

    #./7 !2API "(DB )-  !&  5
  12.   10

  13.    10  

  14.    10  8

  15.  1 + 3

  16. "# 1 + 3   !   

        1 
  17. 9:6511 + )431 0% )434"! + '0(/8" 31*&6"  82

    !7$ )4 65,- #.  1" 65
  18.  

  19.  *0.M g1I^]a  c?; MF[MQ*0.:8e_bA!  3GW4GW4C fU>T#A <L!OdVDD!

     4GWE72KBZDI#e+(),/+%J5  " " PHY`N$ 3XV\R93VF[MQ-&0'( 6=PH`N=#@S  
  20. 

  21.  : https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html

  22.  Entity Value Object Domain Service Repository Gateway UseCase UseCase

    Input UseCase Output Converter Presenter ViewModel Dao Request Response Eloquent Database Manager AppService Provider Controller
  23.   18 Entity Value Object Domain Service Repository Gateway

    UseCase UseCase Input UseCase Output Converter Presenter ViewModel Dao Request Response Eloquent Database Manager AppService Provider Controller 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  24. 5Laravel Entity Value Object Domain Service Repository Gateway UseCase UseCase

    Input UseCase Output Converter Presenter ViewModel Dao Request Response Eloquent Database Manager AppService Provider Controller
  25.    1,2    3,4

  26. 

  27. Entity Value Object Domain Service Model

  28. • <GEntityB>;=  • ?1@:3  '+("&7%$ ,.,4 DF0 •

    Value Object68E5 9Value Object/ • I2)#+-AH  C • (*.!&,  Value Object
  29. 8B0<5;0<5101<5+) 3/61  @, 79 4:?->  & $'%"#2 &!$

     A(= value()D;*C.  
  30. Entity • M5;PIDQE%)'!"7 ,( • Value ObjectG83KF>H  • Entity.

    )$ #/<9 +-0&*/  A P1  +-0&*/BLQ • IC:7?N UseCase4O=  +-0&*/ 62J@ DH 
  31. Entity  !  ""#   $  Void

  32. • Model58."+*.C& %!-/-GI  • F> ;?H2ModelJ(, )#'6K  

    • <IRepository 3D7@EA • EntityB=94EA • 0Domain Service1:, .!$+" . *. 3D Domain Service
  33. B+?executeG;.0= 6D+ ' : F-A58 + H6 Domain Service.,Domain Service

    4OK I* $'! *&* # !%(#J  )"@<?21/3 >9F-A5C7 E 
  34. Repository UseCase UseCase Input UseCase Output Service

  35. Repository •    • Interface • Entity 

  36. Entity"=Value Object 7 ! *6 :"=% <#NG EntityA  >&B<

      6/54$9update(Menu $menu) ?3 -) ,  ;:'+(@ 8  1 EntityA  >&B.3 5402#*6void
  37. •  , 0+  ("# • Interface &. •

    Value Object$!  • )-*, Clean Architecture'/%  UseCaseInput
  38. $-UseCase   "*  )+( %!&!   #'int

    string ,
  39. UseCaseOutput • #(*8?7  !"'=,- • +,- Interface1;@  :3Interface

      A • Entity 04 Value Object%)&"$.< • 5968Clean Architecture2>/
  40.        

  41.  !%      UseCaseOutput Interface# 

    $ !% " !%
  42. UseCase • ) $#"'(&(  03 • !%5.*  •

    UseCaseInput12UseCaseOutput6,  • RepositoryDomainService+/ 4 7- ("' (&(
  43. Domain Service   execute     Domain

    Service Repository  
  44. *># 34;217UseCase  / 6-;   UseCase  ,

    1. Repository&+ 2. Repository&+ 3. Domain Service*9 4. Domain Service*9 5. Entity5.'$ 6. Repository 21 7. UseCaseOutput<% "()8 execute=0!:
  45. Gateway Converter Presenter ViewModel Dao Adapter

  46. Gateway • Repository!Implements • @S/:-+0"3.2: B/:-! 14#95/8?EP= • R14#95/8!3.2:?E /:-+0"

    Q M • @S"%,+Dao!<HCNDao!' 9+07%- #9*$%)69 <H • ;DB079(%)69!A &:+    DaoLIM • FKGJClean ArchitectureDao!DO>
  47. (  ,3Dao+ /  -2)  DI  *0

    Eloquent ORM #"( !$'%& 1. 
  48. *8;:1/A #($ '"!)29  &(%(.56@ 0<Gateway-3>4? Dao  Delete Insert

    6@,= +7  ...
  49. Dao • ( % • #  Eloquent Model&) !'

     DatabaseManager*% • API$"  Guzzle
  50.  Eloquent Model     DatabaseManager 

  51. Converter • UseCaseInputImplements  • ;<Clean ArchitectureControllerB/@ Converter31486  •

    2;>Web "$'LaravelRequest+"%*# +! )+  =&(+-.48  • Console5@ Console=Converter, • 9?:>Clean ArchitectureLaravelRequest7A0 @
  52. /0-5&+  ( 2 $ #,$!%$  DI  

    HTTP430 ') "$1') *. 
  53. Presenter • UseCaseOutput68 5 4231=? • HTML.JSON.*% ,!'.)/+.-".Console23#$ &(:> 

    239<7 •  230;
  54.  execute  

  55. Presenter!   ViewModel    JsonPresenter JsonViewModel 

      
  56. ViewModel •    • Humble Object  

  57. JsonViewModel    JSON  toArray  

  58.    Json  blade    

  59. JsonPresenter JsonViewModel       { “id”

    => 3, “name” => “aaa”, “settings” => { “key” => “value”, … } }
  60. Humble Object    

  61.     JSON   HTMLUI !" 

      
  62. ViewModel'* & &+(  "$% !$#) %   

    Humble Object%$, 
  63.    Entity Value Object Domain Service Repository Gateway

    UseCase UseCase Input UseCase Output Converter Presenter ViewModel Dao Request Response Eloquent Database Manager AppService Provider Controller
  64.  Entity Value Object Domain Service Repository Gateway UseCase UseCase

    Input UseCase Output Converter Presenter ViewModel Dao Request Response Eloquent Database Manager AppService Provider Controller
  65. Controller Application

  66. • ApplicationJ#W&ControlnCRo$kG • K[c'*-7:6/03ConverterUseCase Presenter^MNh&h • @?Aa&$H<WI49;!mj "%$ kGLb !#

    #$ • >_SNhX,:1).2TFc\g nDIo&B#Y$  \gFcU&dQeVl  36(:Pi`D fH= " $] $ • UseCaseNhO'58+;-7:_Z Aa&E"%$ Controller
  67. "&*  2, ) Converter, UseCase, Presenter ($,, !#Laravel'+ 

     %*-(  
  68. AppServiceProvider       

  69. .=1 '49+8 3<- Laravel Middleware*=  ;> 2 )%$ UseCase

    .=06!%(#'/ '& "' 7: , 5
  70.   

  71. Routing Controller    Controller!    

    1. Application 
  72. '% RoutingController 4+ 'Controller>, 7: ,6 2< ; 1. Application

    =- 'Controller $&#! 15*09 "&&. && (3 'UseCase /; 8)2<  ; 2. Action Method /;
  73. +)$RoutingController 90 +Controller D1 <?1; 7A @ 1. ApplicationB2 +Controller(*'

    "% 6: /5> &*#*3  *!*,8 +UseCase4@=-7A  @ 2. Action Method4@ +UseCaseInputUseCase executeE:. 4@ +UseCaseOutputC  3. UseCase4@
  74. /- #(RoutingController >5 /ControllerI6AD6@ <FE 1. ApplicationG7 /Controller ",.+$&) ;?4:C*.'.!8

     .#%.#0= /UseCase9EB2<F E 2. Action Method9E /UseCaseInputUseCase executeJ?3 9E  /UseCaseOutputH  3. UseCase9E /UseCaseOutputPresenter executeJ?3 9E  /return  ControllerH1 H 4. Presenter9E
  75. None
  76.  

  77. ;1M5F%&( • 5@U'-*XLX?NB2H3!G  • O4<:XLX?.,EBR"! XL330DV! YTWB8A / 9C

    Z • IJ)#$+  > 6Q   >S7=K   4B8 &$&$@P 
  78. NWYP • ;X>"4,'A  YP  !0%8 &( • YPYF17M<9-815GTEDB

    O YP faker"= CZ YP • ;@RU.3$6I9RU#*2)8IHV B?L.3$6IHV/,+:SHV B?L  JKHVQD0%8&( 
  79. 

  80. '.>73:(/ ,+ • LaravelArtisan!*0$'.3:!*0$3 •  2,3;?3<24-#10!"%A B @93:C8 

    7D Controller!&)= 56  
  81. PHPStan/LaraStanPhan;N • JWE#= $YOTK Clean Architecture <GLI 9I<  (8+1,/5&#*82

    • PhanMFcomposer.json&UV Phan24)7C TK&  • LaraStancomposer.json&UV' 24)7CA (.3-8CTK>N  • PhpStanLaraStan!#P(.3-8CLaravelQTK PhanS$24)7CTK& " • ZS# $9086B: $RDXLI @#  &LIH %$ ?[
  82. 

  83. • 07'528'/5+&3_ I"!CB ,:3US LV !!ZE]H$K, 468) R ! •

    ;N%I" O? #1&.*A ^ !a XPb`"`G@[  • Clean Architecture;WY=$XM !>D9JFQc<G (*-3!T\ 
  84.