为什么某些 return 任务的方法在通用存储库模式中未标记为异步
Why are some methods that return a Task not marked as Async in generic repository pattern
我正在阅读一篇关于使用以下内容创建通用异步存储库的非常酷的文章 link https://blog.zhaytam.com/2019/03/14/generic-repository-pattern-csharp/ 该接口将所有操作定义为任务,但实现选择不使用 async/await模式上的几种方法。我想进一步了解这一点,所以决定在此处 post。乍一看,客户端似乎不知道他们需要等待未标记为异步的方法,但我可能没有正确理解这一点。任何人都可以评论为什么作者选择不在某些 return 任务而不是其他方法上使用异步?
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
}
注意方法有 async
也有 await
。在那些方法中你等待执行得到结果,同时在那些没有await
的方法中你不关心它们什么时候被执行
我怀疑这是一个意外遗漏。
我很难看出等待某些基本 class 方法而不是其他方法有什么好处。
仅使用提供的代码,public async Task Add(T entity)
方法除外,"async/await" 的使用似乎完全是随机的。
在所有情况下,您可以将方法转换为 async
并在 return 之前放置一个 await
,或者您可以删除 async
和 awaits
和 return Task
.
最终,结果是相同的,无论是编译代码还是 运行 时间的行为(主要是性能)
我正在阅读一篇关于使用以下内容创建通用异步存储库的非常酷的文章 link https://blog.zhaytam.com/2019/03/14/generic-repository-pattern-csharp/ 该接口将所有操作定义为任务,但实现选择不使用 async/await模式上的几种方法。我想进一步了解这一点,所以决定在此处 post。乍一看,客户端似乎不知道他们需要等待未标记为异步的方法,但我可能没有正确理解这一点。任何人都可以评论为什么作者选择不在某些 return 任务而不是其他方法上使用异步?
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
}
注意方法有 async
也有 await
。在那些方法中你等待执行得到结果,同时在那些没有await
的方法中你不关心它们什么时候被执行
我怀疑这是一个意外遗漏。
我很难看出等待某些基本 class 方法而不是其他方法有什么好处。
仅使用提供的代码,public async Task Add(T entity)
方法除外,"async/await" 的使用似乎完全是随机的。
在所有情况下,您可以将方法转换为 async
并在 return 之前放置一个 await
,或者您可以删除 async
和 awaits
和 return Task
.
最终,结果是相同的,无论是编译代码还是 运行 时间的行为(主要是性能)