Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In CQRS, how do you build the response when creating an entity?

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?

like image 408
yowmamasita Avatar asked Jan 26 '17 06:01

yowmamasita


People also ask

How does CQRS pattern work?

CQRS separates reads and writes into different models, using commands to update data, and queries to read data. Commands should be task-based, rather than data centric. ("Book hotel room", not "set ReservationStatus to Reserved").

What is CQRS pattern in Microservices?

CQRS is one of the important pattern when querying between microservices. We can use CQRS design pattern in order to avoid complex queries to get rid of inefficient joins. CQRS stands for Command and Query Responsibility Segregation. Basically this pattern separates read and update operations for a database.


1 Answers

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();
    }
like image 116
Artur Kędzior Avatar answered Oct 04 '22 19:10

Artur Kędzior