Base solution for your next web application
Open Closed

Why GetAll() isn't Aync? #299


User avatar
0
alexnaldo created

Hi,

Why only GetAll() doesn't have a async method?

Thanks.


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Because it's impossible. First of all, only EF supports Async. So, async is not general. Second; IQueryable does not support async and it can not support. Async is used at the end (while executing the query), for example you can use GetAll().Where(...).ToListAsync(); So, ToList method has async version as ToListAsync. As similar, there is a FirstOrDefaultAsync and so on... You can use ToListAsync() if you add EntityFramework reference to your project.

  • User Avatar
    0
    alexnaldo created

    I understood. I read the sources and others methods uses GetAll() but return Async, so works as expected.

    Tks.

  • User Avatar
    0
    radek stromský created

    It would be enough to add support for projection for FirstOrDefaultAsync and GetAllListAsync. That should cover most of the scenarios. Overload would cause a problems, so designed the interface methods like this:

    Task<TModel> QueryFirstOrDefaultAsync<TModel>(Func<IQueryable<TEntity>, IQueryable<TModel>> projection);
    Task<List<TModel>> QueryToListAsync<TModel>(Func<IQueryable<TEntity>, IQueryable<TModel>> projection);
    

    Usage example:

    var result = await _myRepo.QueryFirstOrDefaultAsync(q =>  q.Where(x => x.Id == id).ProjectTo<MyDto>());
    

    or without using AutoMapper's IQuerable extension:

    var result = await _myRepo.QueryFirstOrDefaultAsync(q =>  q.Where(x => x.Id == id).Select(x => x.Status));
    

    Such implementation for EF repository is quite straightforward. Not sure about NHibernate.

    What is your opinion on this?

    PS: I can contribute if we agree on this solution. I am also opened to any suggestions how should final method names look like.

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    Instead of adding methods to repository, we can add extension methods to IQueryable<T>. Because, other people will need to CountAsync, SingleAsync.. and so on. This can be good but I could not find a good naming yet. Because, if we name it as "ToListAsync" then it will conflict with EF's "ToListAsync" when we have EF reference.

    Let's continue this discussion on Github. Can you create an issue for it by referencing this forum post? Thanks.