[英]Why are some methods that return a Task not marked as Async in generic repository pattern
I was reading a really cool article about creating a generic Async repository using the following link https://blog.zhaytam.com/2019/03/14/generic-repository-pattern-csharp/ The interface defines all operations as tasks but the implementation chooses not to use the async/await pattern on a few methods.我正在阅读一篇关于使用以下链接创建通用异步存储库的非常酷的文章https://blog.zhaytam.com/2019/03/14/generic-repository-pattern-csharp/该接口将所有操作定义为任务,但实现选择在一些方法上不使用 async/await 模式。 I'd like to further my understanding of this so decided to post on here.我想进一步了解这一点,所以决定在这里发帖。 At first glance, it would seem like the client may not know they need to wait for methods that are not marked async, but I probably don't understand this correctly.乍一看,似乎客户端可能不知道他们需要等待未标记为异步的方法,但我可能没有正确理解这一点。 Can anyone comment as to why the author choose not to use async on some methods that return a task and not others?任何人都可以评论为什么作者选择不在某些返回任务而不是其他方法的方法上使用异步?
public interface IAsyncRepository<T> where T : BaseEntity
{
Task<T> GetById(int id);
Task<T> FirstOrDefault(Expression<Func<T, bool>> predicate);
Task Add(T entity);
Task Update(T entity);
Task Remove(T entity);
Task<IEnumerable<T>> GetAll();
Task<IEnumerable<T>> GetWhere(Expression<Func<T, bool>> predicate);
Task<int> CountAll();
Task<int> CountWhere(Expression<Func<T, bool>> predicate);
}
public class EfRepository<T> : IAsyncRepository<T> where T : BaseEntity
{
#region Fields
protected DataDbContext Context;
#endregion
public EfRepository(DataDbContext context)
{
Context = context;
}
#region Public Methods
public Task<T> GetById(int id) => Context.Set<T>().FindAsync(id);
public Task<T> FirstOrDefault(Expression<Func<T, bool>> predicate)
=> Context.Set<T>().FirstOrDefaultAsync(predicate);
public async Task Add(T entity)
{
// await Context.AddAsync(entity);
await Context.Set<T>().AddAsync(entity);
await Context.SaveChangesAsync();
}
public Task Update(T entity)
{
// In case AsNoTracking is used
Context.Entry(entity).State = EntityState.Modified;
return Context.SaveChangesAsync();
}
public Task Remove(T entity)
{
Context.Set<T>().Remove(entity);
return Context.SaveChangesAsync();
}
public async Task<IEnumerable<T>> GetAll()
{
return await Context.Set<T>().ToListAsync();
}
public async Task<IEnumerable<T>> GetWhere(Expression<Func<T, bool>> predicate)
{
return await Context.Set<T>().Where(predicate).ToListAsync();
}
public Task<int> CountAll() => Context.Set<T>().CountAsync();
public Task<int> CountWhere(Expression<Func<T, bool>> predicate)
=> Context.Set<T>().CountAsync(predicate);
#endregion
}
Notice that the methods have async
also have await
.请注意,这些方法有async
也有await
。 In those methods you wait for the to execute to obtain the result, meanwhile in those methods without await
you don't care when they will be executed在那些方法中,您等待执行以获取结果,同时在那些没有await
的方法中,您不在乎它们何时执行
My suspicion is that it was an accidental omission.我怀疑这是一个意外遗漏。
I am having a hard time seeing any benefit to awaiting some of the base class methods and not the others.我很难看到等待一些基本 class 方法而不是其他方法有什么好处。
Using ONLY the code presented, with the exception of the public async Task Add(T entity)
method, the utilisation of "async/await" seems to be completely random.仅使用提供的代码,除了public async Task Add(T entity)
方法外,“异步/等待”的使用似乎是完全随机的。
In all cases, you can either transform the method to async
and put a await
right before returning, OR you can remove the async
and awaits
and return the Task
.在所有情况下,您都可以将方法转换为async
并在返回之前放置一个await
,或者您可以删除async
和awaits
并返回Task
。
In the end, the result is the same, both in terms of compiled code as in behaviour in run-time (performance mostly)最后,结果是相同的,无论是编译代码还是运行时的行为(主要是性能)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.