rbasniak
rbasniak

Reputation: 4954

Returning objects on CQRS commands with MediatR

I have been reading about MediatR and CQRS latelly and I saw many people saying that commands shouldn't return domain objects. They can return values but they're limited to returning erros values, failure/success information and the Id of the newly created entities.

My question is how to return this new objetct to the client if the command can return only the Id of the new entity.

1) Should I query the database again with this new Id? If so, isn't that bad that I making a new trip to the database to get an object that was in the memory a few seconds ago?

2) What's the correct way of returning the entities created by the commands?

Upvotes: 1

Views: 1918

Answers (1)

Henk Mollema
Henk Mollema

Reputation: 46501

I think the more important question is why you shouldn't return domain objects from commands. If the reason for that seems like a valid reason for you, you should look into alternatives such as executing a query right after the command to fetch the domain object.

If, however, returning the domain object from the command fits your needs and does not impose any direct problems, then why not just do it and keep things simple and straightforward?

Upvotes: 3

Related Questions