UseCaseã®åå©ç¨æ§
Clean Architectureã«ã¯UseCase層ãå®ç¾©ããã¦ãã¾ããããã®UseCaseãä¸ä½ã©ããããã®ãªã®ã度ã ããããªããªãã®ã§ãèªåã®èããã¾ã¨ãã¦ã¿ãã¨ã³ããªã§ãã
Clean Architectureã«ã¤ãã¦ã¯ãã¡ã
æ¥æ¬èªè¨³ï¼クリーンアーキテクチャ(The Clean Architecture翻訳)
以éãæ¦å¿µãâã¦ã¼ã¹ã±ã¼ã¹âãå®è£ ãããã¢ããâUseCaseâã¨è¡¨è¨ãããã¨ã«ãã¾ãã ï¼åãã£ã¡ãåããªãã§ãããæãã¦ããã®ãã¨ããã©ããå¤ããã®ã§è¡¨è¨åãããã¦ãã¾ããï¼ ã¾ããWebã¢ããªã±ã¼ã·ã§ã³ãæ³å®ãã¦ãã¾ãã
ã¦ã¼ã¹ã±ã¼ã¹ã¨ã¯ä½ãªã®ã
Clean Architectureããæç²ãã¾ãã
Use Cases
The software in this layer contains application specific business rules. It encapsulates and implements all of the use cases of the system. These use cases orchestrate the flow of data to and from the entities, and direct those entities to use their enterprise wide business rules to achieve the goals of the use case.
We do not expect changes in this layer to affect the entities. We also do not expect this layer to be affected by changes to externalities such as the database, the UI, or any of the common frameworks. This layer is isolated from such concerns.
We do, however, expect that changes to the operation of the application will affect the use-cases and therefore the software in this layer. If the details of a use-case change, then some code in this layer will certainly be affected.
ã¦ã¼ã¹ã±ã¼ã¹ã¯ã¢ããªã±ã¼ã·ã§ã³åºæã®ãã¸ãã¹ã«ã¼ã«ãã«ãã»ã«åãããã®ã§ãã
ã¨ã³ãã£ãã£ï¼ãã¡ã¤ã³ãªãã¸ã§ã¯ãï¼ã®æã¤ï¼ä¼æ¥å
¨ä½ã®ï¼ãã¸ãã¹ã«ã¼ã«ã使ã£ã¦ããããã®èª¿æ´ï¼ãã¼ã¿ã®åã渡ãçï¼ãè¡ããã¨ã§ã¦ã¼ã¹ã±ã¼ã¹ã®éæãç®æãã¾ãã
ã¦ã¼ã¹ã±ã¼ã¹ã¯ããã¾ã§ã¨ã³ãã£ãã£ãï¼ä¸æ¹çã«ï¼å©ç¨ããå´ã§ããããããã®å±¤ã§ã®å¤æ´ï¼ã¢ããªã±ã¼ã·ã§ã³ã®å¤æ´ï¼ã¯ã¨ã³ãã£ãã£ã«å½±é¿ãä¸ããªããã¨ãæã¾ããã§ãã
ã¾ããããå¤å±¤ã®DBãUIãªã©ããã®å½±é¿ãããéé¢ããã¦ãããã¨ãæã¾ããã§ãã
ã¿ãããªãã¨ãClean Architectureã§ã¯æ¸ããã¦ãã¾ãã
ã¬ã¤ãã©ã¤ã³ããã¦ã¼ã¹ã±ã¼ã¹ãèãã
以åãã¦ã¼ã¹ã±ã¼ã¹é£æ°ã«ãªã£ã¦ããèªãã æ¸ç±ãã¦ã¼ã¹ã±ã¼ã¹é§åéçºå®è·µã¬ã¤ããã§ã¯ã以ä¸ã®æ§ãªã¬ã¤ãã©ã¤ã³ãè¿°ã¹ããã¦ãã¾ãã
- ï¼ç»é¢ã®ãããªï¼ãã¦ã³ããªã¯ã©ã¹ã®ååã使ããªããã
- ãã¡ã¤ã³ã¯ã©ã¹ã®ååã使ããªããã
- ãåè©ï¼ä¸»èªï¼ - åè© - åè©ï¼ç®çèªï¼ãï¼æ¥æ¬èªã§ãªããåè©ï¼ä¸»èªï¼ - åè©ï¼ç®çèªï¼ - åè©ãï¼ã¨ããæã®æ§é ã«å¾ã£ã¦ã¦ã¼ã¹ã±ã¼ã¹ãæ¸ããªããã
- ãªãã¸ã§ã¯ãã¢ãã«ã®è¨èã使ã£ã¦ã¦ã¼ã¹ã±ã¼ã¹ãæ¸ããªããã
- ã¦ã¼ã¹ã±ã¼ã¹ã¯å®è¡æã®æ¯ãèãã®ä»æ§ã§ããã¨ãããã¨ãå¿ããªãããã«ã
- GUIãããã¿ã¤ããç»é¢ã¢ãã¯ã¢ããã使ããªããã
- ã¤ãã³ãã¨ãã®å¿çã®æµãã¨ãã¦ã¦ã¼ã¹ã±ã¼ã¹ãæ¸ããã¦ã¼ã¶ã¨ã·ã¹ãã ã®å¯¾è©±ã®ä¸¡å´ãè¨è¿°ããªããã
- ã¦ã¼ã¹ã±ã¼ã¹ã¯åè¿°çã«æ¸ããªããã
- ã¢ã¯ã¿ã¼ã¨ã¦ã¼ã¹ã±ã¼ã¹å³ã使ã£ã¦ã¦ã¼ã¹ã±ã¼ã¹ãçµç¹åããªããã
- 2段è½ã«ã¼ã«ã«å¾ããªããã
ä¸ç¹ã注æãå¿
è¦ãªã®ã¯ãã¦ã¼ã¹ã±ã¼ã¹è¨è¿°ã¨ã¦ã¼ã¹ã±ã¼ã¹å³ã¯ç°ãªãã¨ãããã¨ã§ãã
ãã®ã¬ã¤ãã©ã¤ã³ã¯ã¦ã¼ã¹ã±ã¼ã¹è¨è¿°ã®ã¬ã¤ãã©ã¤ã³ã§ãã
ã§ãããã¦ã¼ã¹ã±ã¼ã¹å³ã®ç²åº¦ï¼ä¸è¨ã§è¡¨ç¾ãããã®ï¼ã¯å
¨ç¶éããã¨ããã¨ãæ°ãã¤ãããã¨ã¯åãã§ããã¦ã¼ã¹ã±ã¼ã¹å³ã¯ã¦ã¼ã¹ã±ã¼ã¹è¨è¿°ã®ç®æ¬¡ã§ãã
UseCaseã¯ãã®ã¦ã¼ã¹ã±ã¼ã¹è¨è¿°ããã¨ã«å®è£
ãã¾ãã*1
è¨ãæããã¨ãUseCaseã«ä½ãå«ããã°è¯ããã¯ãã¦ã¼ã¹ã±ã¼ã¹è¨è¿°ãæ¸ãã¦ã¿ãã¨ãããããããã¨ãããã¨ã§ããã
ã¦ã¼ã¹ã±ã¼ã¹è¨è¿°ã¯ããã¦ã¼ã¶ãAãè¡ãã¨ãã·ã¹ãã ã¯Xãå¿çãããã¦ã¼ã¶ã¯Xã確èªãã¦Bãè¡ããã·ã¹ãã ã¯Bã確èªãã¦Yãå¿çããã......ãã®ããã«ã¦ã¼ã¶ï¼ã¢ã¯ã¿ã¼ï¼ã¨ã·ã¹ãã ã®ç¸äºä½ç¨ãè¨è¿°ãããã®ã§ãã ãããã¦ã¼ã¹ã±ã¼ã¹ãå®è£ ããéã®é£ãããªã®ã§ã¯ãªããã¨æã£ã¦ãã¾ãã ã·ã¹ãã ãå¿çããæç¹ã§HTTPã®ãããªã¹ãã¼ãã¬ã¹ãªãããã³ã«ã§ã¯åºåãã¨ãªããããä¸ã¤ã®UseCaseã¨ãã¦å®è£ ãã¥ããããã§ãã
ä¸æ¹ã§ãã¦ã¼ã¹ã±ã¼ã¹è¨è¿°ã®æéã¯ãã¾ãå¤ããªããªãããã«ãã¾ããï¼2段è½ã«ã¼ã«ï¼ ããã«å¾ãéãã¯ã¦ã¼ã¹ã±ã¼ã¹ã®ç²åº¦ã¯ã¢ã¯ã¿ã¼ãã·ã¹ãã ã使ãã·ã³ãã«ãªæä½ã«ã¨ã©ã¾ããã¨ã«ãªãã§ãããã ããã¨ãã·ã¹ãã ã使ã£ã¦ä½ãã®ç®çãéæããããã«ã¦ã¼ã¹ã±ã¼ã¹ã®åå¾é¢ä¿ããããããã¦ã¼ã¹ã±ã¼ã¹å³ã«ãã£ã¦ãã®é¢ä¿æ§ãå³ç¤ºããããã¾ãã
ããããåããã®ã¯ãã¦ã¼ã¹ã±ã¼ã¹ã¨ä¸å£ã«è¨ã£ã¦ããã¦ã¼ã¹ã±ã¼ã¹ãå¼ã³åºãã¦ã¼ã¹ã±ã¼ã¹ãããããã¨ãããã¨ã§ãã
ã¦ã¼ã¹ã±ã¼ã¹å³ã§ã¯ invokes ã¨ãã¦é¢ä¿ã表ãã¾ãã
UseCaseã§å®è£
ãã対象ã¯ããããã®å¤§ãªãå°ãªãã®ã¦ã¼ã¹ã±ã¼ã¹ã«ãªãã¨èããã¨ãUseCase層ã®ãªãã§ãä»ã®UseCaseãå¼ã³åºããã¨ã¯æãå¾ããã§ãã
â»å¿è«ãããã¦ã¼ã¹ã±ã¼ã¹ã¯ããã¦ã¼ã¹ã±ã¼ã¹ã®å¾ã«ããããã¨ãããã®ãããã¾ããå
¸åçãªãã®ã¯ãã°ã¤ã³ãªã©ã§ãããã¦ã¼ã¹ã±ã¼ã¹é§åéçºå®è·µã¬ã¤ãã§ã¯ <<precedes>>
㨠<<invokes>>
ã§ã¦ã¼ã¹ã±ã¼ã¹ã®é¢ä¿ã¯ååã¨ãã¦ãã¾ãã
è¯ãã¦ã¼ã¹ã±ã¼ã¹ã¨ã¯
åºæ¬çã«ã¯ã¬ã¤ãã©ã¤ã³ã®éããªã®ã§ããããã¤ã³ãã«ãªãè¦ç¹ã ãæ´çãã¦ããã¾ãã
åèï¼良いユースケースを書くための発想法
- å©ç¨è ï¼ã¢ã¯ã¿ã¼ï¼ããã®è¦ç¹ã§è¨è¿°ãããã¨
- ã¦ã¼ã¹ã±ã¼ã¹ã¨ã¯ãã¢ã¯ã¿ã¼ãã·ã¹ãã ã使ã£ã¦ä½ãã§ããããã表ãããã®ã§ãããã¨
ã¦ã¼ã¹ã±ã¼ã¹ã®ç®çã¯ãã·ã¹ãã ã使ã£ã¦ä½ãã§ããªãã®ãã決ãããã¨ã«ããã¾ãã
ã©ã®ãããªã¢ã¯ã·ã§ã³ããã£ãã¨ããã·ã¹ãã ã¯ã©ã®ãããªå¿çãããã®ããã¨ããå¢çã決ãããã¨ã«ããã¾ãã
æ¸ç±ãã¦ã¼ã¹ã±ã¼ã¹é§åéçºå®è·µã¬ã¤ããã§ã代æ¿ã³ã¼ã¹ï¼æ³å®ãããã¦ã¼ã¶ã®éåãªæä½ãªã©ï¼ãå¯è½ãªéãæ¾ããã¨ãæ¨å¥¨ããã¦ãã¾ãã
ããã¯ç·å¼ãæ確ã«ããããã«å¿
è¦ãªåæã§ãã
ã¦ã¼ã¹ã±ã¼ã¹ã®ã¬ãã«
æ¸ç±ãUMLã¢ããªã³ã°ã®ã¨ãã»ã³ã¹ãã§ã¯Cockburnã®"ã¦ã¼ã¹ã±ã¼ã¹ã®ã¬ãã«"ãåãä¸ãã¦ãã¾ãã
- æµ·é¢ã¬ãã«
主ã¢ã¯ã¿ã¼ã¨ã·ã¹ãã éã®åã ã®ç¸äºä½ç¨ã表ãã¾ãããã®ãããªã¦ã¼ã¹ã±ã¼ã¹ã¯ä¸»ã¢ã¯ã¿ã¼ã«ãªãããã®ä¾¡å¤ãä¸ãããã®ã§ã主ã¢ã¯ã¿ã¼ã¯ä¸è¬çã«æ°åãã30åç¨åº¦ããã¦ã¦ã¼ã¹ã±ã¼ã¹ãå®è¡ãã¾ãã - æµ·ä¸ã¬ãã«
æµ·é¢ã¬ãã«ã®ã¦ã¼ã¹ã±ã¼ã¹ã«å å«ããã¦ããã¨ããçç±ã ãã§åå¨ããã¦ã¼ã¹ã±ã¼ã¹ã§ãã - ä¸ç©ºã¬ãã«
æµ·é¢ã¬ãã«ã®ã¦ã¼ã¹ã±ã¼ã¹ãããã«åºç¯å²ãªãã¸ãã¹ä¸ã®ç¸äºä½ç¨ã®ä¸ã§å ããä½ç½®ã示ãã¾ãã
ä¸ç©ºã¬ãã«ã®ã¦ã¼ã¹ã±ã¼ã¹ã¯ãé常ãã¸ãã¹ã¦ã¼ã¹ã±ã¼ã¹*2ã§ãããæµ·é¢ããã³æµ·ä¸ã¬ãã«ã¯ã·ã¹ãã ã¦ã¼ã¹ã±ã¼ã¹*3ã§ããã½ããã¦ã§ã¢ã®è¦³ç¹ã§ã¯ã¦ã¼ã¹ã±ã¼ã¹ã¯æµ·é¢ã¬ãã«ã«ãã¾ãã
ä½è«ï¼includes
ã¦ã¼ã¹ã±ã¼ã¹ã®ä¸ã«è¤éãªã¹ããããããå ´åãªã©ã¯ãå å«ï¼includeï¼ãã¨ãè¨ãã¾ãããã¦ã¼ã¹ã±ã¼ã¹é§åéçºå®è·µã¬ã¤ãã§ã¯ä¸è¦ã¨ãã¦åãæ¨ã¦ã¦ã¾ãã*4 ã¾ããæ¸ç±ãUMLã¢ããªã³ã°ã®ã¨ãã»ã³ã¹ãã§ãã以ä¸ã®ããã«è¿°ã¹ããã¦ãã¾ãã
ã¦ã¼ã¹ã±ã¼ã¹ã®å å«ã¯ããã®ã¾ã¾ã§ã¯ã¡ã¤ã³ã·ããªãªããããã«ãããªããããªè¤éãªã¹ãããããè¤æ°ã®ã¦ã¼ã¹ã±ã¼ã¹ã§ç¹°ãè¿ãããã¹ãããã«ä½¿ç¨ããã¨ä¾¿å©ã§ããããããæ©è½ã®åå²ã«ãã£ã¦ã¦ã¼ã¹ã±ã¼ã¹ããµãã¦ã¼ã¹ã±ã¼ã¹ããµããµãã¦ã¼ã¹ã±ã¼ã¹ã«åããã¨ãã£ããã¨ã¯ããªãã§ãã ããããã®ãããªåå²ã¯å¤å¤§ãªæéã®æµªè²»ã§ãã
åå©ç¨æ§ã¯ããã®ã
ã¦ã¼ã¹ã±ã¼ã¹ã¯ãã¦ã¼ã¶ï¼ã¢ã¯ã¿ã¼ï¼ã¨ã·ã¹ãã ã®å¯¾è©±ããã¼ã¹ã«ããã¾ãã
ã¦ã¼ã¹ã±ã¼ã¹ã¯åºæ¬çã«ã¯åå©ç¨ããããã®ã§ã¯ãªãã¨èãã¦ãã¾ãã
ãªããªããå ´é¢ããã¹ã¦éããã®ã§ããã¯ãã ããã§ãã
ãã ããåè¿°ã®ããã«ã¦ã¼ã¹ã±ã¼ã¹å³ãæ¸ããã¨ããinvokesã®é¢ä¿ï¼ã¨precedesã®é¢ä¿ï¼ãç¾ãããã¨ãããã¾ãã
åå©ç¨ãããã¨ããè¦æ±ã¯åè¿°ã® invokes ãåããå´ã®ã¦ã¼ã¹ã±ã¼ã¹ã«å¯¾ãã¦èµ·ãããã®ã®ããã«æãã¾ãã
ãã¦ã¼ã¹ã±ã¼ã¹ã®ã¬ãã«ãç¯ã®è©±ã使ãã°ãåè ã¯æµ·é¢ã¬ãã«ãå¾è ã¯æµ·ä¸ã¬ãã«ãã¨ãªãã§ãããã æµ·é¢ã¬ãã«ã®ã¦ã¼ã¹ã±ã¼ã¹ã¯åå©ç¨ãããã®ã§ã¯ãªããæµ·ä¸ã¬ãã«ã®ã¦ã¼ã¹ã±ã¼ã¹ã¯ä»ã®ã¦ã¼ã¹ã±ã¼ã¹ããå©ç¨ããããã¨è¨ããã®ã§ã¯ãªãããªã¨æãã¾ãã
ã¦ã¼ã¹ã±ã¼ã¹ã®ç¬ç«æ§
ä¸åã®ã¦ã¼ã¹ã±ã¼ã¹ã¨ãã¦ç¬ç«æ§ã¯ä¿ã¤ãã¨ãéè¦ã¨èãã¦ãã¾ãã ä¾ãã°ãå ã Aã¨ããã¦ã¼ã¹ã±ã¼ã¹ããã£ã¦B, Cã®ã¦ã¼ã¹ã±ã¼ã¹ããinvokesãããã¨ãã¾ãã ããã§ã¢ããªã±ã¼ã·ã§ã³ã¸ã®è¦æ±ãå¤ãããAãä¿®æ£ããå¿ è¦ãåºãã¨ããããã¯ã¼ã¯ããã¼ï¼BâAï¼ã§ã¯ä¿®æ£ãå¿ è¦ã§ããä¸æ¹ã®ã¯ã¼ã¯ããã¼ï¼CâAï¼ã§ã¯ä»¥åã®åä½ã§ããå¿ è¦ããããã¨ããå ´åãAã¯ç°ãªãã¦ã¼ã¹ã±ã¼ã¹ã¨ããæ¹ãè¯ãããä¸èããå¿ è¦ãããã¨æãã¾ãã ãªããªããã¦ã¼ã¹ã±ã¼ã¹ã¯ã¹ãããã®éåã§ããããã®ã¹ãããã«å·®ããããªãç°ãªãç®çããã£ãã¦ã¼ã¹ã±ã¼ã¹ã§ããã¨æãããããã§ãããã®å·®ããã¦ã¼ã¹ã±ã¼ã¹ã®"æ¡å¼µ"ã¨ãã¦è¨±å®¹ã§ãããããå°ãã妥å½ãªãã®ãªãè¯ãã§ãããæ²¹æããã¨"æ¡å¼µ"ã¹ããããå¢å¤§ãããã¨ã«ãªãããã¾ããã
ç¾å®åé¡ãã¦ã¼ã¹ã±ã¼ã¹ãè¨ããã§å°ã
ã¨ãããã¨ãããããããªãããªã¼ã¨æãã¾ãã
ã¦ã¼ã¹ã±ã¼ã¹ã¯ã·ã¹ãã å å¤ã®ç¸äºä½ç¨ã表ããã®ã§ããã®ã§ãããä¸ã¤ã®æä½ã§å¶ãããã·ã¹ãã ã®æ¯ãèãã大ãããã°ãã¦ã¼ã¹ã±ã¼ã¹ã大ãããªããããªæ°ããã¾ãã ã¾ãã¯ãã¦ã¼ã¹ã±ã¼ã¹ã®ç®çãå¤ããããã¨ãçãã¹ãã¨ã¯æãã¾ãããæ¨ä»ã®ãããã¯ãã¯éæ³ã®ã¯ã³ã¯ãªãã¯*5ãæã¦ã¯ãããã¦ãããã®ã§ãã¦ã¼ã¹ã±ã¼ã¹ã«å«ã¾ããã¹ããããå¤ãã¯ãªã£ã¦ãã¾ãã®ããããã¾ããã
ã§ã¯ããã®ã¨ãã©ããããã§ããããã¢ã¯ã¿ã¼ã¯ã¦ã¼ã¶ã§ããå¿ è¦ã¯ãªããã¨ããç¹ããã³ãã«ãªãããããã¾ããã ã¦ã¼ã¹ã±ã¼ã¹ã«ãããã¢ã¯ã¿ã¼ã¯å½¹å²ã¨å¼ã¶æ¹ãããæ£ããã§ãã ã§ãã®ã§ãä¾ãã°ãã¦ã¼ã¶ã®ä»£ããã«ã·ã¹ãã ã¨ç¸äºä½ç¨ãè¡ãã¢ã¯ã¿ã¼ãç¨æãã¦ãã¦ã¼ã¹ã±ã¼ã¹ãå°ãããã¦ãããã¨ããã¢ããªã³ã°ã使ããå¯è½æ§ãããã¾ãã
ã³ã¼ãã«ããã¨ãã©ããªãã®ã ãã
ä½ããããµã³ãã«ã³ã¼ããåºãã¤ããã ã£ããã§ããåå°½ãã¾ãã......ã ç¶ãï¼ããããã¾ããï¼
ã¾ã¨ã
ã¦ã¼ã¹ã±ã¼ã¹ã«ã¯ã¬ãã«ãããã æµ·ä¸ã¬ãã«ã®ã¦ã¼ã¹ã±ã¼ã¹ã¯åå©ç¨ãããå¯è½æ§ãããã ã ãã©ãã¦ã¼ã¹ã±ã¼ã¹ã¯æ¥µåç¬ç«ãã¦ããæ¹ãè¯ãã¨æãã®ã§ãåå©ç¨ã§ã¯ãªãç®çã責åãä¿ããããã¨ãç®æããã
åè
*1:ãã ããã¦ã¼ã¹ã±ã¼ã¹é§åéçºå®è·µã¬ã¤ãã§è¿°ã¹ãããICONIXã§ã¯ã¦ã¼ã¹ã±ã¼ã¹è¨è¿°ããããã¹ããã¹åæãè¡ããã·ã¼ã±ã³ã¹å³ã«æ¸ãåºãã¦å®è£ ã«ã¤ãªãã¾ãã
*2:顧客ãã¤ãã³ãã«å¯¾ãããã¸ãã¹ä¸ã®å¯¾å¿ãåé¡ã«ããã
*3:ã½ããã¦ã§ã¢ã¨ã®ããåãã«é¢ããã
*4:ä¸æ¹ã§UMLã¢ããªã³ã°ã®ã¨ãã»ã³ã¹ã§ããextendã¯ç¡è¦ããã¨åãæ¨ã¦ã¦ãããinvokesãåºã¦ããªãã®ã§ãã¦ã¼ã¹ã±ã¼ã¹å³ã«ããã¦ã¯å³å¯ãªé¢ä¿å®ç¾©ãããåå¾ãåãããã¨ã®æ¹ãéè¦ã§ãã
*5:å°ãªãæä½ã§å¤ãã®å¹æãå¾ãããã«