dotn30471 2017-12-18 15:31
浏览 65
已采纳

实现CQRS时如何在控制器中获取创建的对象

I am evaluating the CQRS pattern and wonder what would be the best way to obtain an Entity created by a command in the same action so I can render it in the view.

The two options I can think of are.

1) Create an id in the controller and send it with the command to fetch the entity by finding it by id. 2) Create an instance of the entity and send it with the command so I have a reference to it after it's populated

Example code

public function createEntityAction(array $data) {
    $eventDispatcher = $this->get('event_dispatcher');

    $eventDispatcher->dispatch(
        CreateEntityHandler::name, // Handler
        new Entity($data) // Command
    );

    // Placeholder //
      $entity = get-the-created-entity
    // // 

    return $this->view($entity, Response::HTTP_OK);    
}
  • 写回答

1条回答 默认 最新

  • dongzi0857 2017-12-27 16:07
    关注

    Second option is not really an option. "Entity creation", which is in fact is a business operation, is a command handling.

    Generally speaking, the one who sends a command, whose handler creates an entity, should send the entity id with it. In what way the identity is generated is just an implementation concern.

    Usually, command handlers either do what they suppose to do and return nothing (or ACK) or throw (or NAK).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器