You need to enable JavaScript to run this app. 垂直切片架构下Controller、Command/Query等组件职责划分咨询
优惠活动
大模型
产品
解决方案
定价
更多

垂直切片架构下Controller、Command/Query等组件职责划分咨询

垂直切片架构四类核心组件职责说明

1. Controller

职责:仅负责HTTP层的请求处理,不包含任何业务逻辑。具体工作包括:

  • 接收前端传入的请求参数,做基础的格式校验(比如参数是否缺失、字段类型是否正确)
  • 将校验后的参数封装为对应的 Command(写操作)或 Query(读操作)对象
  • 调用对应 CommandHandler 执行实际业务逻辑
  • 将 Handler 返回的执行结果封装为 Response 对象后返回给前端

代码示例(PHP):

class AddFavoriteProductController extends AbstractController
{
    public function __invoke(Request $request, AddFavoriteProductCommandHandler $handler): JsonResponse
    {
        // 基础参数校验
        $this->validate($request, [
            'product_id' => 'required|integer',
            'user_id' => 'required|integer'
        ]);

        // 构造Command对象
        $command = new AddFavoriteProductCommand(
            productId: $request->get('product_id'),
            userId: $request->get('user_id')
        );

        // 调用Handler执行业务逻辑
        $result = $handler->handle($command);

        // 封装Response返回
        return new JsonResponse(new AddFavoriteProductResponse($result));
    }
}

2. Command/Query

职责:纯数据传输对象(DTO),不包含任何业务逻辑、数据库操作。其中Command对应写操作(新增、修改、删除),Query对应读操作,仅用于存储执行对应操作所需的全部参数。
注意:不要在这个类里实现Doctrine的数据库操作,这部分逻辑要放到Handler中。

代码示例(PHP):

class AddFavoriteProductCommand
{
    public function __construct(
        public readonly int $productId,
        public readonly int $userId
    ) {}
}

3. CommandHandler(QueryHandler同理)

职责:对应单个Command/Query的业务逻辑执行单元,遵循单一职责原则,一个Handler仅处理一类操作。具体工作包括:

  • 接收Command/Query对象作为入参
  • 执行业务规则校验(比如收藏商品前判断商品是否存在、用户是否已经收藏过该商品)
  • 调用ORM(如Doctrine)或其他服务完成数据库读写、第三方调用等操作
  • 返回业务处理结果给Controller

代码示例(PHP):

class AddFavoriteProductCommandHandler
{
    public function __construct(
        private readonly ProductRepository $productRepository,
        private readonly FavoriteRepository $favoriteRepository
    ) {}

    public function handle(AddFavoriteProductCommand $command): bool
    {
        // 业务校验:商品是否存在
        $product = $this->productRepository->find($command->productId);
        if (!$product) {
            throw new \RuntimeException('商品不存在');
        }

        // 业务校验:是否已收藏
        $exists = $this->favoriteRepository->existsByUserAndProduct($command->userId, $command->productId);
        if ($exists) {
            throw new \RuntimeException('该商品已在收藏列表中');
        }

        // 执行收藏操作
        $this->favoriteRepository->add($command->userId, $command->productId);

        return true;
    }
}

4. Response

职责:纯数据载体,仅用于定义返回给前端的字段结构,不直接做JSON序列化操作。统一返回字段格式,避免散落在Controller各处的字段拼接逻辑。

代码示例(PHP):

class AddFavoriteProductResponse implements \JsonSerializable
{
    public function __construct(
        public readonly bool $success,
        public readonly string $message = ''
    ) {}

    public function jsonSerialize(): array
    {
        return [
            'success' => $this->success,
            'message' => $this->message
        ];
    }
}

完整调用流程

请求进入 → Controller校验参数、封装Command → 调用CommandHandler执行业务逻辑 → 处理结果返回给Controller → Controller封装为Response返回前端

内容的提问来源于stack exchange,提问作者Vahe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.09 01:30:03