我对领域驱动的设计概念相当陌生,我遇到了一个问题,即在使用命令总线和域逻辑的命令和命令处理程序时,在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);
}
}
Run Code Online (Sandbox Code Playgroud)
在我们的API中,我们有一个用于创建帖子的端点.这将在我们的Application目录下的PostController中路由createPost方法.
final class PostController
{
private $commandBus;
public function …Run Code Online (Sandbox Code Playgroud)