question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Create Command then Read Query

See original GitHub issue

Hi,

Is it okay to call a create command and then the read to get the newly created record? (I have some loosely coupled objects which require some additional mapping to get the related entities)

public async Task<IActionResult> Post([FromBody] Create.Command command)
{
    if (!ModelState.IsValid) return BadRequest(ModelState);
    var result = await _mediator.Send(command);
    return Ok(_mediator.Send(new GetById.Query {Id = result.Id}));
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
GabrielHSFerreiracommented, Feb 17, 2021

I guess it have no problem since both calls are being made on your application entry point. What you should avoid is call Mediator.Send inside a Handler that responds to a previous Mediator.Send.

0reactions
OculiViridicommented, Jun 3, 2021

@TDK1964 “RESTful-ly” speaking, a Controller method that creates an entity should return the created entity’s Id. And also, the corresponding HTTP status response should be 201 Created. Why not changing the implementation of your Create.Command, to persist the entity and then return the corresponding Id, so you can avoid the “ugly” 2 calls to Mediator?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Should CQRS Command perform a Query?
My question is in the command handler, is it okay to perform queries to get the data to build the aggregate roots I...
Read more >
How to run command-line SQLite query and exit?
Just include the command in quotes after the database file argument. For example, the following creates a table called abc :
Read more >
c# - Select query to get data from SQL Server
For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on...
Read more >
CQRS pattern - Azure Architecture Center
CQRS stands for Command and Query Responsibility Segregation, a pattern that ... The read model uses the events to create a snapshot of...
Read more >
A Using SQL Command Line
This section provides an introduction to SQL Command Line (SQL*Plus), an interactive and batch command-line query tool that is installed with Oracle Database ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found