title: Creating an Async version of a Generic Repository
body: <p>I have a Generic repository implementing the following interface :</p>
  
  <pre><code>public interface IRepository
  {
    IUnitOfWork UnitOfWork { get; }
  
  
    IEnumerable&lt;TEntity&gt; GetWithRawSql&lt;TEntity&gt;(string query, params object[] parameters) where TEntity : class;
  
    TEntity GetByKey&lt;TEntity&gt;(object keyValue) where TEntity : class;
  
    IQueryable&lt;TEntity&gt; GetQuery&lt;TEntity&gt;() where TEntity : class;
  
    IQueryable&lt;TEntity&gt; GetQuery&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate) where TEntity : class;
  
    IQueryable&lt;TEntity&gt; GetQuery&lt;TEntity&gt;(ISpecification&lt;TEntity&gt; criteria) where TEntity : class;
  
    TEntity Single&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; criteria) where TEntity : class;
  
    TEntity Single&lt;TEntity&gt;(ISpecification&lt;TEntity&gt; criteria) where TEntity : class;
  
    TEntity First&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; predicate) where TEntity : class;
  
    TEntity First&lt;TEntity&gt;(ISpecification&lt;TEntity&gt; criteria) where TEntity : class;
  
    void Add&lt;TEntity&gt;(TEntity entity) where TEntity : class;
  
    void Attach&lt;TEntity&gt;(TEntity entity) where TEntity : class;
  
    void Delete&lt;TEntity&gt;(TEntity entity) where TEntity : class;
  
    void Delete&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; criteria) where TEntity : class;
  
    void Delete&lt;TEntity&gt;(ISpecification&lt;TEntity&gt; criteria) where TEntity : class;
  
    void Update&lt;TEntity&gt;(TEntity entity) where TEntity : class;
  
    IEnumerable&lt;TEntity&gt; Find&lt;TEntity&gt;(ISpecification&lt;TEntity&gt; criteria) where TEntity : class;
  
    IEnumerable&lt;TEntity&gt; Find&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; criteria) where TEntity : class;
  
    TEntity FindOne&lt;TEntity&gt;(ISpecification&lt;TEntity&gt; criteria) where TEntity : class;
  
    TEntity FindOne&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; criteria) where TEntity : class;
  
    IEnumerable&lt;TEntity&gt; GetAll&lt;TEntity&gt;() where TEntity : class;
  
    IEnumerable&lt;TEntity&gt; Get&lt;TEntity, TOrderBy&gt;(Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderBy, int pageIndex,
      int pageSize, SortOrder sortOrder = SortOrder.Ascending) where TEntity : class;
  
    IEnumerable&lt;TEntity&gt; Get&lt;TEntity, TOrderBy&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; criteria,
      Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderBy, int pageIndex, int pageSize,
      SortOrder sortOrder = SortOrder.Ascending) where TEntity : class;
  
    IEnumerable&lt;TEntity&gt; Get&lt;TEntity, TOrderBy&gt;(ISpecification&lt;TEntity&gt; specification,
      Expression&lt;Func&lt;TEntity, TOrderBy&gt;&gt; orderBy, int pageIndex, int pageSize,
      SortOrder sortOrder = SortOrder.Ascending) where TEntity : class;
  
    int Count&lt;TEntity&gt;() where TEntity : class;
  
    int Count&lt;TEntity&gt;(Expression&lt;Func&lt;TEntity, bool&gt;&gt; criteria) where TEntity : class;
  
    int Count&lt;TEntity&gt;(ISpecification&lt;TEntity&gt; criteria) where TEntity : class;
  }
  </code></pre>
  
  <p>This interface belongs to the modified version of a generic repository as implemented by <a href="http://huyrua.wordpress.com/2010/07/13/entity-framework-4-poco-repository-and-specification-pattern/" rel="nofollow"> huyrua's blog</a>.
  I need to have async version of some of the methods. For instance I have the following implementation for GetWithRawSql method:</p>
  
  <pre><code>public IEnumerable&lt;TEntity&gt; GetWithRawSql&lt;TEntity&gt;(string query, params object[] parameters) where TEntity : class
  {
      return DbContext.Set&lt;TEntity&gt;().SqlQuery(query, parameters).ToList();
  }
  </code></pre>
  
  <p>and for the Async version I have implemented the following:</p>
  
  <pre><code>public async Task&lt;IEnumerable&lt;TEntity&gt;&gt; GetWithRawSqlAsync&lt;TEntity&gt;(string query, params object[] parameters) where TEntity : class
  {
      return await Task.Run(() =&gt; GetWithRawSql&lt;TEntity&gt;(query, parameters));
  }
  </code></pre>
  
  <p>or the same for GetByKey method :</p>
  
  <pre><code>public async Task&lt;TEntity&gt; GetByKeyAsync&lt;TEntity&gt;(object keyValue) where TEntity : class
  {
      return await Task.Run(() =&gt; GetByKey&lt;TEntity&gt;(keyValue));
  }
  </code></pre>
  
  <p>Above implementations looks like a quick and dirty work around. How would you implement above methods and what problems I might encounter using </p>
  
  <pre><code>await Task.Run(() =&gt; GetWithRawSql&lt;TEntity&gt;(query, parameters));
  </code></pre>
  
  <p>in my repository? For full implantation you can have a look at the blog and for your information I use Entity framework 6.1.1.</p>