Talk about the implementation of Clean Architecture again

July 18 18:20~19:41

截圖 2022-07-18 下午12.43.06

▲Figure 1: A recent picture from the book “Clean Architecture”

foreword

Figure 1 is a picture recently implemented in the book “Clean Architecture”. Although Teddy started to implement Clean Architecture mainly by referring to this picture, Teddy always felt that there was something wrong with this picture. At the beginning, only a few obvious problems were seen, such as:

  • Hierarchy is unclear : Is Data Access on the third or fourth tier? If it is the third layer, why violate the dependency principle to directly access the database? If it is the fourth layer, why directly implement the Data Access Interface in the second layer across the layers?
  • Who will maintain the cross-layer principle : The Data Access Interface directly refers to the innermost Entity, so who is responsible for the conversion when the Entity leaves the Use Case Layer and is transmitted to the outside world?
  • Inconsistent terminology : This diagram overemphasizes the relationship between Use Case Interactor and Input Boundary, Output Boundary, and Controller and Presenter, ignoring other Boundary. For example, Data Access Interface should also be an Output Boundary, but instead of using the name Output Boundary, it uses the very specific name of Data Access Interface.
    This phenomenon leads readers to think that Output Boundary and Data Access Interface are two different concepts, but in fact, according to the hexagonal structure, they should both be Output Port.

***

Teddy worked hard to finish reading the book yesterday because he wanted to write a recommendation for the new translated book “Clean Architecture in Action: Getting Your Hands on Clean Architecture” (scheduled to be published in August). The English version of this book was read by Teddy more than a year ago, and he also wrote a book review: < [One less book] Get Your Hands Dirty on Clean Architecture >. But after more than a year, some details in the book are no longer memorized. Last night, I carefully compared the practice in the book with Figure 1. I suddenly felt that Teddy was restricted by Figure 1 before. ezKanban’s current method of applying Clean Architecture can be further optimized. . Today, let’s talk about how to simplify the implementation of Clean Architecture.

***

more mines

In addition to the problems mentioned above, in “Clean Architecture”, the terms of Input Boundary and Output Boundary in Figure 1 are used for the interface of Use Case Layer to communicate with the outside world, but in Figure 22.1 in the book (please refer to Figure 1). 2) The lower right corner uses Use Case Input Port and Use Case Output Port , which are relatively close to the hexagonal architecture (using port). But regardless of Figure 1 or Figure 2, the expression of the relationship between Use Case Interactor and Use Case Input Port and Use Case Output Port is easy to misunderstand the following points:

  • Looking at Figures 1 and 2, one might think that the only object that implements the Use Case Output Port is the Presenter, but in fact a Use Case Interactor can use any Output Port. For example, storing objects in the database is also an Output Port.
  • What exactly is Use Case Input Port? Is it Command? Or the interface of Use Case itself?
  • In Figure 1, it feels that Input Boundary only needs to define Input Data, and Output Boundary defines Output Data. But for an interface, the interface will include input data (Input Data) and return data (Output Data), such as the interface int getFileSize(File file), where File is Input Data, and int is Output Data.

Clear-Architecture

▲Figure 2: Figure 22.1 in the book “Clean Architecture”

***

see pictures and tell stories

Figure 3 is Teddy’s revised version of Figure 1. The key points are as follows:

  • There are only two interfaces: Input Port (Input Boundary) and Output Port (Output Boundary).
  • Each Input Port and Output Port has Input Data and Output Data (declared input and output data on the interface).
  • Presenter does not need to implement Output Port, it can build a Read Model as long as it receives the Output Data of the Input Port. Let the Controller directly couple with the Presenter, and pass the Input Port’s output data directly to the Presenter. Use Case Interactor does not need to be injected into the Presenter.
  • In the case of applying CQRS, Projection (an Output Port) can directly produce the View Model required by the front-end without going through the Presenter.

截圖 2022-07-18 下午7.06.42

▲Figure 3: Corrected Figure 1

***

Program example

Teddy spent four hours changing the course example of [Domain Driven Design and Clean Architecture Introduction] ( https://teddysoft.tw/courses/clean-architecture/) to a new way of writing, and felt that the code was cleaner some. Next, compare the program example with Figure 3. First, refer to Figure 4, which is the interface declaration of CreateCardUseCase in ezKanban, which is equivalent to the Input Port in Figure 3. Where CreateCardInput is Input Data and CqrsCommandOutput is Output Data.

截圖 2022-07-18 下午7.15.26

▲Figure 4: Input Port

Figure 5 shows the package structure, and the files of the Input Port are placed in the in package. Because CqrsCommandOutput is a common object of the system core, it does not appear in Figure 5. In addition, CreateCardUseCaseImpl is the Use Case Interactor in Figure 3, and some people will take the name CreateCardUseCaseService.

截圖 2022-07-18 下午7.21.24

▲Figure 5: Package structure

The CreateCardUseCaseImpl code is shown in Figure 6. It implements Input Port (CreateCardUseCase) and also uses an Output Port (CardRepository).

截圖 2022-07-18 下午7.27.42

▲Figure 6: CreateCardUseCaseImpl code

Referring to the architecture in Figure 3, and with the program example, Teddy feels that it is much more specific than the approach mentioned in the original “Clean Architecture” book.

***

Business Services

[ Introduction to Domain-Driven Design and Simple Architecture Implementation Class ] In the course enrollment in August 2022, the content of this echelon will use the latest simplified implementation of Clean Architecture as a course example, and you are welcome to make more use of the old rain and the new knowledge.

***

Youzo’s inner monologue: Continuous improvement is not just talking about it.

This article is reprinted from https://teddy-chen-tw.blogspot.com/2022/07/clean-architecture.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment