Async GetMany method in Repository

802 views Asked by At

I have repository method which returns collection according to filter

public IEnumerable<T> GetMany(Expression<Func<T, bool>> filter = null)
{
    IQueryable<T> query = DbSet;

    if (filter != null)
    {
        query = query.Where(filter);
    }

    return query.ToList();
}

Now I want to write this method as Async. And this is my problem. Should I change type of DbSet (DbSet<T> ) to something else or what is the correct solution?

protected readonly DbSet<T> DbSet;

public Repository(AdminDbContext context)
{
    this.Context = context;
    this.DbSet = context.Set<T>();
}

UPD : return query.ToListAsync(); - is it enough ? Thanks

1

There are 1 answers

0
Steve Lillis On BEST ANSWER

As specified in this MSDN article, you still use DbSet, but you use async extensions for accessing the set.

Here's an async version of your code:

public Task<IEnumerable<T>> GetManyAsync(Expression<Func<T, bool>> filter = null)
{
    IQueryable<T> query = DbSet;

    if (filter != null)
    {
        query = query.Where(filter);
    }

    return query.ToListAsync();
}