简体   繁体   English

为什么某些返回任务的方法在通用存储库模式中未标记为异步

[英]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 ,或者您可以删除asyncawaits并返回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.

相关问题 为什么有些异步方法需要Task的返回类型,而另一些则不需要 - Why do some async methods require a return type of Task, and others don't 如何在可查询的通用存储库中使用Async方法? - How to use Async methods of in a queryable generic repository? 如果可以编写我们自己的等待,那么为什么异步方法只能返回Task <T>,Task和void? - If it's possible to write our own awaitables, then why can async methods only return Task<T>, Task and void? 为什么 C# 编译器处理空异步任务方法与仅返回 Task.CompletedTask 不同? - Why does the C# compiler handle empty async Task methods different to just return Task.CompletedTask? 实体框架通用存储库模式异步添加方法和保存 - Entity Framework Generic Repository Pattern Async Add Method with Save 任务返回值,没有任务 <T> (异步/等待模式) - Task return value, without Task<T> (async/await pattern) 应该返回Task的API中的方法是否以Task或Async结尾 - Should methods in an API that return Task end with Task or Async 为什么WCF异步方法中有Response返回类型? - Why there are Response return types in WCF async methods? 为什么某些异步方法具有异步而其他异步方法却没有 - Why some async methods have async while other don't UnitOfWork模式的通用存储库模式 - Generic Repository Pattern with UnitOfWork Pattern
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM