Home Generic repository - return Task or async Task?
Reply: 0

Generic repository - return Task or async Task?

user1299
1#
user1299 Published in April 26, 2018, 1:45 pm

This question already has an answer here:

  • Should I use async if I'm returning a Task and not awaiting anything 2 answers

I'm just working on generic repository and I found this sample https://cpratt.co/truly-generic-repository/

I've noticed that some of methods return async Task and some just Task.

e.g.

public virtual Task<bool> GetExistsAsync(Expression<Func<TEntity, bool>> filter = null)
{
    return GetQueryable(filter).AnyAsync();
}

and

public virtual async Task<TEntity> GetByIdAsync(object id)
{
    return await dbSet.FindAsync(id);
}

What's the preference? I have read quite a lot about it and I've seen recommendation to await only if I do something else after await keyword -> this would mean that GetByIdAsync shouldn't have async/await but then it won't be asynchronous method.

If it won't be asynchronous method then what's the benefit of returning just Task like in GetExistsAsync?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.499298 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO