Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get ID in create when applying CQRS?

My take on CQRS is when followed strictly your commands don't return anything (return type void), so my example is really straight forward: How do you retrieve an ID when creating something?

For example, when creating a credit card transaction it seems rather important to return a transaction ID, or when creating a customer it would be much easier if you got the customer you created or the customer ID back so a browser could navigate automatically to that customer page for example.

One solution could be to first ask for an ID and then create the customer or transaction with that ID, but it seems pretty weird.

Does anyone have any experience with this or know how it should be done in the most effective way? Maybe I have misunderstood something?

like image 560
Tomas Jansson Avatar asked Dec 05 '10 23:12

Tomas Jansson


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 C#?

What is CQRS? CQRS stands for Command and Query Responsibility Segregation. It is a command query pattern and can separate the read and write operations for a data store. CQRS uses a command and query pattern in which the command functionality only updates or writes data and the query functionality returns the data.

What does CQRS stand for?

Understanding the CRQS pattern The Command Query Responsibility Segregation (CQRS) pattern separates a service's write tasks from its read tasks. While reading and writing to the same database is acceptable for small applications, distributed applications operating at web-scale require a segmented approach.


1 Answers

CQRS is all about fire-and-forget, and since GUIDs are very reliable (low risk of collision) there is no problem sending in a GUID that you generate your self.

The steps would basically be:

  1. Create your command
  2. Generate and assign your identity (GUID) to it
  3. Fire the command
  4. Return the identity earlier generated

Read more about GUIDs on Wikipedia

like image 180
Robin Orheden Avatar answered Oct 12 '22 14:10

Robin Orheden