php - 如何从命令总线取回数据?

标签 php domain-driven-design api-design command-pattern

我对域驱动设计概念还很陌生,在使用带有域逻辑命令和命令处理程序的命令总线时,我遇到了在 API 中返回正确响应的问题。

假设我们正在使用领域驱动设计方法构建应用程序。我们有后端和前端部分。后端具有我们所有的域逻辑和公开的 API。前端使用 API 向应用程序发出请求。

我们正在使用映射到命令总线的命令和命令处理程序来构建我们的领域逻辑。在我们的域目录下,我们有一个用于创建名为 CreatePostCommand 的帖子资源的命令。它通过命令总线映射到其处理程序 CreatePostCommandHandler。

final class CreatePostCommand
{
    private $title;
    private $content;

    public function __construct(string $title, string $content)
    {
        $this->title = $title;
        $this->content= $content;

    }

    public function getTitle() : string
    {
        return $this->title;
    }

    public function getContent() : string
    {
        return $this->content;
    }
}

final class CreatePostCommandHandler
{
    private $postRepository;

    public function __construct(PostRepository $postRepository)
    {
        $this->postRepository = $postRepository;
    }

    public function handle(Command $command)
    {
        $post = new Post($command->getTitle(), $command->getContent());
        $this->postRepository->save($post);
    }
}

在我们的 API 中,我们有一个用于创建帖子的端点。这是在我们的应用程序目录下的 PostController 中路由的 createPost 方法。

final class PostController
{
    private $commandBus;

    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }

    public function createPost($req, $resp)
    {
        $command = new CreatePostCommand($command->getTitle(), $command->getContent());
        $this->commandBus->handle($command);

        // How do we get the data of our newly created post to the response here?

        return $resp;
    }
}

现在在我们的 createPost 方法中,我们希望在我们的响应对象中返回我们新创建的帖子的数据,以便我们的前端应用程序可以知道新创建的资源。 这很麻烦,因为我们知道根据定义,命令总线不应返回任何数据。所以现在我们陷入了一个令人困惑的境地,我们不知道如何将我们的新帖子添加到响应中对象。

我不确定如何从这里开始解决这个问题,我想到了几个问题:

  • 是否有一种优雅的方式在响应中返回帖子的数据?
  • 我是否错误地实现了 Command/CommandHandler/CommandBus 模式?
  • 这仅仅是 Command/CommandHandler/CommandBus 模式的错误用例吗?

最佳答案

首先,请注意,如果我们将 Controller 直接连接到命令处理程序,我们将面临类似的问题:

    public function createPost($req, $resp)
    {
        $command = new CreatePostCommand($command->getTitle(), $command->getContent());
        $this->createPostCommandHandler->handle($command);

        // How do we get the data of our newly created post to the response here?
        return $resp;
    }

总线引入了一个间接层,允许您将 Controller 与事件处理程序分离,但您遇到的问题更为根本。

I'm not sure how to proceed with this problem from here

TL;DR - 告诉域使用什么标识符,而不是询问域使用了什么标识符。

    public function createPost($req, $resp)
    {
        // TADA
        $command = new CreatePostCommand($req->getPostId()
                 , $command->getTitle(), $command->getContent());

        $this->createPostCommandHandler->handle($command);

        // happy path: redirect the client to the correct url
        $this->redirectTo($resp, $postId)
    }

简而言之,客户端,而不是域模型或持久层,负责生成新实体的 id。应用程序组件可以读取命令本身中的标识符,并使用它来协调下一个状态转换。

在此实现中,应用程序只是将消息从 DTO 表示形式转换为域表示形式。

另一种实现使用命令标识符,并从该命令派生出将要使用的身份

        $command = new CreatePostCommand(
                 $this->createPostId($req->getMessageId())
                 , $command->getTitle(), $command->getContent());

Named UUIDs在后一种情况下是常见的选择;它们是确定性的,并且碰撞概率很小。

现在,这个答案有点作弊——我们实际上只是证明了在这种情况下我们不需要来自命令处理程序的结果。

一般来说,我们更愿意拥有一个; Post/Redirect/Get 是用于更新域模型的好习惯用法,但是当客户端获取资源时,我们希望确保他们获得的版本包含他们刚刚所做的编辑。

如果您的读取和写入使用的是同一本记录簿,这不是问题——无论您读取什么,始终是可用的最新版本。

然而,是领域驱动设计中的一种常见架构模式,在这种情况下,写入模型(处理帖子)将重定向到读取模型——通常发布陈旧数据。因此,您可能希望在获取请求中包含最低版本,以便处理程序知道刷新其陈旧的缓存。

Is there an elegant way to return the post's data in the response?

您随问题提供的代码示例中有一个示例:

public function createPost($req, $resp)

想一想:$req 是 http 请求消息的表示,大致类似于您的命令,而 $resp 本质上是一个数据结构的句柄,您可以将结果写入其中。

换句话说,用您的命令传递回调或结果句柄,并让命令处理程序填写详细信息。

当然,这取决于你的总线是否支持回调;不保证。

另一种不需要更改命令处理程序签名的可能性是安排 Controller 订阅命令处理程序发布的事件。您协调 correlation id在命令和事件之间,并使用它来拉出您需要的结果事件。

细节并不重要——处理命令时生成的事件可以写入消息总线,或复制到邮箱,或....

关于php - 如何从命令总线取回数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150637/

相关文章:

php - 如何在 YAML 中使用 API 平台过滤器?

logging - DDD。域模型和日志记录

c# - Entity Framework Core 2.1 - 自有类型和嵌套值对象

java - 针对不同包中的相同类

ios - viewDidLoad 方法命名是否具有误导性?

PHP:多个 SELECT COUNT 与

php - SQL选择最近日期的多列

php - mysql_data_seek 和 mysql_field_seek 之间的区别?

language-agnostic - 使用 DDD 和 CQRS 处理域逻辑的重复

rest - 网站 API 的黄金标准是什么?推特、Flickr、 Facebook 等