title: CQRS: Command Return Values
body: <p>There seems to be endless confusion about whether commands should or should not have return values.  I would like to know if the confusion is simply because the participants have not stated their context or circumstances.</p>
  <h1>The Confusion</h1>
  <p>Here are examples of the confusion...</p>
  <ul>
  <li><p>Udi Dahan says commands are &quot;not returning errors to the client,&quot; but <a href="http://udidahan.com/2009/12/09/clarified-cqrs/" rel="noreferrer">in the same article</a> he shows a diagram where commands indeed return errors to the client.</p>
  </li>
  <li><p>A Microsoft Press Store article states &quot;the command...doesn't return a response&quot; but then goes on to give an ambiguous caution:</p>
  </li>
  </ul>
  <blockquote>
  <p>As battlefield experience grows around CQRS, some practices consolidate and tend to become best practices. Partly contrary to what we just stated... it is a common view today to think that both the command handler and the application need to know how the transactional operation went. Results must be known...</p>
  </blockquote>
  <ul>
  <li>Jimmy Bogard says &quot;<a href="https://lostechies.com/jimmybogard/2013/12/19/put-your-controllers-on-a-diet-posts-and-commands/" rel="noreferrer">commands always have a result</a>&quot; but then makes extra effort to show how commands return void.</li>
  </ul>
  <p>Well, do command handlers return values or not?</p>
  <h1>The Answer?</h1>
  <p>Taking a cue from Jimmy Bogard's &quot;<a href="https://lostechies.com/jimmybogard/2012/08/22/busting-some-cqrs-myths/" rel="noreferrer">CQRS Myths</a>,&quot; I think the answer(s) to this question depends on what programmatic/contextual &quot;quadrant&quot; you are speaking of:</p>
  <pre><code>+-------------+-------------------------+-----------------+
  |             | Real-time, Synchronous  |  Queued, Async  |
  +-------------+-------------------------+-----------------+
  | Acceptance  | Exception/return-value* | &lt;see below&gt;     |
  | Fulfillment | return-value            | n/a             |
  +-------------+-------------------------+-----------------+
  </code></pre>
  <h3>Acceptance (e.g. validation)</h3>
  <p>Command &quot;Acceptance&quot; mostly refers to validation.  Presumably validation results must be given synchronously to the caller, whether or not the command &quot;fulfillment&quot; is synchronous or queued.</p>
  <p>However, it seems many practitioners don't initiate validation from within the command handler.  From what I've seen, it is either because (1) they've already found a fantastic way to handle validation at the application layer (i.e. an ASP.NET MVC controller checking valid state via data annotations) or (2) an architecture is in place which assumes commands are submitted to an (out of process) bus or queue.  These latter forms of asynchrony generally don't offer synchronous validation semantics or interfaces.</p>
  <p>In short, many designers might want the command handler to provide validation results as a (synchronous) return value, but they must live with the restrictions of they asynchrony tools they are using.</p>
  <h3>Fulfillment</h3>
  <p>Regarding &quot;fulfillment&quot; of a command, the client who issued the command might need to know the scope_identity for a newly created record or perhaps failure information - such as &quot;account overdrawn.&quot;</p>
  <p>In a real-time setting it seems that a return value makes the most sense; exceptions should not be used to communicate business-related failure outcomes.  However, in a &quot;queuing&quot; context...return values naturally make no sense.</p>
  <p>This is where all the confusion can perhaps be summarized:</p>
  <blockquote>
  <p>Many (most?) CQRS practitioners assume they will now, or in the future, incorporate an asynchrony framework or platform (a bus or queue) and thus proclaim that command handlers do not have return values.  However, some practitioners have no intention of using such event-driven constructs, and so they will endorse command handlers that (synchronously) return values.</p>
  </blockquote>
  <p>So, for example, I believe a synchronous (request-response) context was assumed when <a href="https://lostechies.com/jimmybogard/2013/12/19/put-your-controllers-on-a-diet-posts-and-commands/" rel="noreferrer">Jimmy Bogard provided this sample command interface</a>:</p>
  <pre><code>public interface ICommand&lt;out TResult&gt; { }
  
  public interface ICommandHandler&lt;in TCommand, out TResult&gt;
      where TCommand : ICommand&lt;TResult&gt;
  {
      TResult Handle(TCommand command);
  }
  </code></pre>
  <p>His Mediatr product is, after all, an in-memory tool.  Given all this, I think the reason Jimmy <a href="https://lostechies.com/jimmybogard/2013/12/19/put-your-controllers-on-a-diet-posts-and-commands/" rel="noreferrer">carefully took the time to produce a void return from a command</a> was not because &quot;command handlers should not have return values,&quot; but instead because he simply wanted his Mediator class to have a consistent interface:</p>
  <pre><code>public interface IMediator
  {
      TResponse Request&lt;TResponse&gt;(IQuery&lt;TResponse&gt; query);
      TResult Send&lt;TResult&gt;(ICommand&lt;TResult&gt; query);  //This is the signature in question.
  }
  </code></pre>
  <p>...even though not all commands have a meaningful value to return.</p>
  <h1>Repeat and Wrap Up</h1>
  <p>Am I correctly capturing why there is confusion on this topic?  Is there something I'm missing?</p>
  <h1>Update (6/2020)</h1>
  <p>With the help of the answers given, I think I've untangled the confusion.  Put simply, if a CQRS command is capable of returning a success/failure indicating <em>completion</em> status, then a return value makes sense.  This includes returning a new DB row identity, or any result that does not read or return domain model (business) content.</p>
  <p>I think where &quot;CQRS command&quot; confusion emerges, is over the definition and role of &quot;asynchrony&quot;.  There is a big difference between &quot;task based&quot; async IO, and an asynchronous architecture (e.g. queue based middle-ware).  In the former, the async &quot;task&quot; can and will provide the completion result for the async command.  However, a command sent to RabbitMQ will not similarly receive a request/reponse completion notification.  It is this latter context of async-architecture that causes some to say &quot;there is no such thing as an async command&quot; or &quot;commands do not return values.&quot;</p>