title: MVC5 Async ActionResult. Is that possible?
body: <p>In our application we have CQRS: we have <code>IAsyncCommand</code> with <code>IAsyncCommandHandler&lt;IAsyncCommand&gt;</code>.</p>
  
  <p>Usually the command is processed via Mediator like this:</p>
  
  <pre><code>var mediator = //get mediator injected into MVC controller via constructor
  var asyncCommand = // construct AsyncCommand
  // mediator runs ICommandValidator and that returns a list of errors if any
  var errors = await mediator.ProcessCommand(asyncCommand); 
  </code></pre>
  
  <p>That works fine. Now I noticed that I do a lot of repetitive code in controller actions:</p>
  
  <pre><code>public async virtual Task&lt;ActionResult&gt; DoStuff(DoStuffAsyncCommand command)
  {
      if (!ModelState.IsValid)
      {
          return View(command);
      }
  
      var result = await mediator.ProcessCommandAsync(command);
  
      if (!result.IsSuccess())
      {
          AddErrorsToModelState(result);
          return View(command);
      }
      return RedirectToAction(MVC.HomePage.Index());
  }
  </code></pre>
  
  <p>And this patterns repeats over and over in many-many controllers. So for single-threaded commands I've done simplification:</p>
  
  <pre><code>public class ProcessCommandResult&lt;T&gt; : ActionResult where T : ICommand
  {
      private readonly T command;
      private readonly ActionResult failure;
      private readonly ActionResult success;
      private readonly IMediator mediator;
  
  
      public ProcessCommandResult(T command, ActionResult failure, ActionResult success)
      {
          this.command = command;
          this.success = success;
          this.failure = failure;
  
          mediator = DependencyResolver.Current.GetService&lt;IMediator&gt;();
      }
  
      public override void ExecuteResult(ControllerContext context)
      {
          if (!context.Controller.ViewData.ModelState.IsValid)
          {
              failure.ExecuteResult(context);
              return;
          }
  
          var handlingResult = mediator.ProcessCommand(command);
  
          if (handlingResult.ConainsErrors())
          {
              AddErrorsToModelState(handlingResult);
              failure.ExecuteResult(context);
          }
  
          success.ExecuteResult(context);
      }
      // plumbing code
  }
  </code></pre>
  
  <p>And after some plumbing done, my controller action looks like this:</p>
  
  <pre><code>public virtual ActionResult Create(DoStuffCommand command)
  {
      return ProcessCommand(command, View(command), RedirectToAction(MVC.HomePage.Index()));
  }
  </code></pre>
  
  <p>This works well for sync-commands where I don't need to do <code>async-await</code> patterns. As soon as I try to do <code>async</code> operations, this does not compile, as there is no <code>AsyncActionResult</code> in MVC (or there is and I can't find it) and I can't make MVC framework use async operations on <code>void ExecuteResult(ControllerContext context)</code>.</p>
  
  <p>So, any ideas how I can make a generic implementation of the controller action I quoted on top of the question?</p>