在CQRS中,如何在创建实体时构建响应? [英] In CQRS, how do you build the response when creating an entity?

查看:62
本文介绍了在CQRS中,如何在创建实体时构建响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用CQRS并创建一个实体,并且其某些属性的值是其构造函数的一部分生成(例如<$ c的默认 active 值$ c> status 属性,或 createdAt 的当前日期时间),如果命令处理程序可以,如何将其作为响应的一部分呢?是否会返回值?

If using CQRS and creating an entity, and the values of some of its properties are generated part of the its constructor (e.g. a default active value for the status property, or the current datetime for createdAt), how do you include that as part of your response if your command handlers can’t return values?

推荐答案

您需要在创建实体之前创建guid,然后使用该guid对其进行查询。这样,您的命令处理程序将始终返回void。

You would need to create guid before creating an entity, then use this guid to query it. This way your command handlers always return void.

    [HttpPost]
    public ActionResult Add(string name)
    {
        Guid guid = Guid.NewGuid();
        _bus.Send(new CreateInventoryItem(guid, name));
        return RedirectToAction("Item", new { id = guid});
    }


    public ActionResult Item(Guid id)
    {
        ViewData.Model = _readmodel.GetInventoryItemDetailsByGuid(id);
        return View();
    }

这篇关于在CQRS中,如何在创建实体时构建响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆