垂直切片架构 - Controller、Command/Query、Response、CommandHandler 中应该有什么?

Vertical Slice Architecture - What should in the Controller, Command/Query, Response, CommandHandler?

经过深思熟虑,我决定将现有的应用程序目录结构重组为垂直切片方法。

为了匹配所需的结构,我更改了这个问题中的项目文件以匹配下面链接的示例并使事情变得清晰。

- Controllers [Based on Request Method, GET, POST, PUT, calls appropriate service method]
  - ProductsController.php
- Data
  - DAL [Data Access Layer - Specific Read / Write Queries]
      - Products
  - Mapper [Maps parameter array to object]
      - ProductsMapper.php
  - Model [Contains classes matching table structure]
      - Product.php
  - Service [Business Logic]
      - ProductsService.php
  - Provider [Database Connection Class]
      - Database.php

因此,经过一些研究,我得出结论,如果我使用垂直切片对我的应用程序结构进行建模并找到一个清晰的示例,这会使事情变得更容易。

Out with the Onion, in With The Vertical Slices

- Features
  - Products
    - AddFavoriteProduct
      - AddFavoriteProductController.php
      - AddFavoriteProductCommand.php
      - AddFavoriteProductResponse.php
      - AddFavoriteProductCommandHandler.php
      - AddFavoriteProductContainer.js
      - AddFavoriteProduct.js
      - AddFavoriteProduct.css
    - GetProductList
      - GetProductListController.php
      - GetProductListQuery.php
      - GetProductListResponse.php
      - GetProductListCommandHandler.php
      - GetProductListContainer.js
      - GetProductList.js
      - GetProductList.css

我面临的问题是我应该向这四个文件中的每一个委托什么。

我的大致理解如下

Controller - 处理请求并调用 command/query,或者这是 CommandHandler?

Command/Query - 这有 insert/update 命令或数据库查询,在我的例子中,将通过像 Doctrine[= 这样的对象关系映射框架来促进14=]

Response - Returns 给用户想要的信息,或者命令状态结果作为 JSON

CommandHandler - 不清楚这里有什么...

正在寻找一些示例片段或一些说明以开始。描述四个组件中每个组件的功能会有所帮助。

The problem I face is what do I delegate to each of these four files.

我建议尝试在两种张力之间找到平衡

  • 您希望经常一起变化的东西靠在一起(即:两个想法在同一个源文件中,因为它们在逻辑上和时间上是耦合的)。
  • 您希望具有不同依赖关系的事物位于不同的文件中。

要审阅的重要论文是Parnas 1971

We propose instead that one begins with a list of difficult design decisions or design decisions which are likely to change. Each module is then designed to hide such a decision from the others.

例如,您的 Controller 可能知道关于 HTTP 和您正在使用的 Web 框架的所有信息。但它可能不知道您的域模型、数据库连接或事务边界。

(据我所知,“四个”文件并没有什么特别神奇的地方;它很容易不止于此,这取决于您如何对设计进行聚类,您希望支持哪些其他类型的接口,等等上。)