【发布时间】:2022-02-17 02:26:48
【问题描述】:
这个问题涉及 CQRS 和 DDD。
我想创建一个帖子评论。这是我的端点: /posts/{postId}/cmets
但首先我需要检查是否存在具有特定 ID 的帖子。在每篇文章中,每本关于 CQRS 和 DDD 的书籍中,每个查询的类名都以 Getxxxxx 开头,并返回数据。我在任何地方都没有找到检查项目是否存在并返回真/假的查询示例,为什么?我想知道是否可以创建一个名为“PostExistsQuery”的查询。 因为在整个互联网上没有类似的例子。 :O 也许我做错了什么? :0
[HttpPost("/posts/{postId}/comments")]
public async Task<IActionResult> CreatePostComment(Guid postId, CreateCommentDTO commentDTO)
{
if (await _mediator.Send(new PostExistsQuery(postId)) == false) // check if a post exists
{
return NotFound();
}
var commentCommand = new CreateCommentCommand(Guid.NewGuid(), postId, commentDTO.Author, commentDTO.Content);
await _mediator.Send(commentCommand);
return CreatedAtAction(nameof(GetCommentById), new { id = commentCommand.CommentId });
}
【问题讨论】:
标签: c# domain-driven-design cqrs