title: Is this the correct usage of async/await in MVC with service/repository layer?
body: <p>Using VS2013, EF6.1.1, MVC5, .net 4.5.</p>
  
  <p>I have just started looking into async/await for the first time and I am not sure if this is doing it properly. It appears to work, but could it be simpler? I seem to be sticking async/await in a lot of places for one method call down multiple layers.</p>
  
  <p>All code has been simplified for brevity.</p>
  
  <p>In my MVC controller action method I have:</p>
  
  <pre><code>public async Task&lt;ActionResult&gt; TestAction1()
  {
      var testResponse = await _testService.TestMethod1(1);
  
      if (testResponse != null)
      {
          _unitOfWork.CommitAsync();
          return View(testResponse);
      }
  
      return RedirectToAction("TestAction2");
  }
  </code></pre>
  
  <p>My service class is as follows:</p>
  
  <pre><code>public class TestService : ITestService
  {
      public async Task&lt;TestObject&gt; TestMethod1()
      {
          var testObject1 = await privateMethod1Async();
          if (testObject1 != null) return testObject1;
  
          testObject1 = await privateMethod2Async();
  
          return testObject1;
      }
  
      private async Task&lt;TestObject&gt; privateMethod1Async()
      {
          return await _testRepository.FirstOrDefaultAsync();
      }
  
      private async Task&lt;TestObject&gt; privateMethod2Async()
      {
          return await _testRepository.FirstOrDefaultAsync();
      }
  }
  </code></pre>
  
  <p>And my repository method:</p>
  
  <pre><code>public async Task&lt;TEntity&gt; FirstOrDefaultAsync()
  {
      return await _entitySet.FirstOrDefaultAsync();
  }
  </code></pre>
  
  <p>Basically, I have a controller method, that calls a service method. The service method is to call the database layer twice, in an async fashion. But I feel that I am changing every single method and layer to deal with async and I wasn't sure if what I have here is correct.</p>
  
  <p>Secondly, in the controller method I am unsure how to call the unit of work commit method asynchronously. The line that has "_unitOfWork.CommitAsync();". I cannot stick an "await" before it as it is a void method.</p>
  
  <p>Any thoughts?</p>
  
  <hr>
  
  <p>EDIT 1</p>
  
  <p>Here is the full version of the repository method call to EF:</p>
  
  <pre><code>public async Task&lt;TEntity&gt; FirstOrDefaultAsync(Expression&lt;Func&lt;TEntity, bool&gt;&gt; 
                                                 predicate, params 
                                                 Expression&lt;Func&lt;TEntity, object&gt;&gt;[]
                                                 includeProperties)
  {
      IQueryable&lt;TEntity&gt; query = EntitySet;
      if (includeProperties != null &amp;&amp; includeProperties.Any())
      {
          query = IncludeProperties(query, includeProperties);
      }
      return await query.FirstOrDefaultAsync(predicate);
  }
  </code></pre>