Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return empty IQueryable in an async repository method

Lets say I have a simple repository class, with one GetByNames method

public class MyRepo
{
    private readonly MyDbContext _db;

    public MyRepo(MyDbContext db)
    {
        _db = db;
    }

    public IQueryable<MyObject> GetByNames(IList<string> names)
    {
        if (names== null || !names.Any())
        {
            return Enumerable.Empty<MyObject>().AsQueryable();
        }

        return _db.MyObjects.Where(a => names.Contains(a.Name));
    }
}

Now when I use it with async EntityFramework ToListAsync() extension

var myObjects = awawit new MyRepo(_db).GetByNames(names).ToListAsync();

It will blow up if I pass in empty list or null because Enumerable.Empty<MyObject>().AsQueryable() does not implement IDbAsyncEnumerable<MyObject> interface.

The source IQueryable doesn't implement IDbAsyncEnumerable. Only sources that implement IDbAsyncEnumerable can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068.

So my question is, how can I return an empty IQueryable<> that implements IDbAsyncEnumerable, without hitting the database?

like image 747
gaiazov Avatar asked Oct 23 '15 14:10

gaiazov


People also ask

What do I replace IQueryable with for async operations?

linq. entity-framework-core.

Can IQueryable return null?

IQueryable. DefaultIfEmpty() marked as non-nullable even though it can return null values as a LEFT JOIN · Issue #69817 · dotnet/runtime · GitHub.

What is FirstOrDefaultAsync in c#?

FirstOrDefaultAsync<TSource>(IQueryable<TSource>)Asynchronously returns the first element of a sequence, or a default value if the sequence contains no elements. C# Copy.

What is AnyAsync?

AnyAsync<TSource>(IQueryable<TSource>) Asynchronously determines whether a sequence contains any elements. AnyAsync<TSource>(IQueryable<TSource>, Expression<Func<TSource,Boolean>>) Asynchronously determines whether any element of a sequence satisfies a condition.


1 Answers

I ended up implementing an extension method that returns wrapper which implements IDbAsyncEnumerable. It is based on this boilerplate implementation for mocking async code.

With this extension method I can use

return Enumerable.Empty<MyObject>().AsAsyncQueryable();

which works great.

Implementation:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;

namespace MyProject.MyDatabase.Extensions
{
    public static class EnumerableExtensions
    {
        public static IQueryable<T> AsAsyncQueryable<T>(this IEnumerable<T> source)
        {
            return new AsyncQueryableWrapper<T>(source);
        }

        public static IQueryable<T> AsAsyncQueryable<T>(this IQueryable<T> source)
        {
            return new AsyncQueryableWrapper<T>(source);
        }
    }

    internal class AsyncQueryableWrapper<T>: IDbAsyncEnumerable<T>, IQueryable<T>
    {
        private readonly IQueryable<T> _source;

        public AsyncQueryableWrapper(IQueryable<T> source)
        {
            _source = source;
        }

        public AsyncQueryableWrapper(IEnumerable<T> source)
        {
            _source = source.AsQueryable();
        }

        public IDbAsyncEnumerator<T> GetAsyncEnumerator()
        {
            return new AsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
        }

        IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator()
        {
            return GetAsyncEnumerator();
        }

        public IEnumerator<T> GetEnumerator()
        {
            return _source.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public Expression Expression => _source.Expression;
        public Type ElementType => _source.ElementType;
        public IQueryProvider Provider => new AsyncQueryProvider<T>(_source.Provider);
    }

    internal class AsyncEnumerable<T> : EnumerableQuery<T>, IDbAsyncEnumerable<T>, IQueryable<T>
    {
        public AsyncEnumerable(IEnumerable<T> enumerable)
            : base(enumerable)
        { }

        public AsyncEnumerable(Expression expression)
            : base(expression)
        { }

        public IDbAsyncEnumerator<T> GetAsyncEnumerator()
        {
            return new AsyncEnumerator<T>(this.AsEnumerable().GetEnumerator());
        }

        IDbAsyncEnumerator IDbAsyncEnumerable.GetAsyncEnumerator()
        {
            return GetAsyncEnumerator();
        }

        IQueryProvider IQueryable.Provider => new AsyncQueryProvider<T>(this);
    }

    internal class AsyncQueryProvider<TEntity> : IDbAsyncQueryProvider
    {
        private readonly IQueryProvider _inner;

        internal AsyncQueryProvider(IQueryProvider inner)
        {
            _inner = inner;
        }

        public IQueryable CreateQuery(Expression expression)
        {
            var t = expression.Type;
            if (!t.IsGenericType)
            {
                return new AsyncEnumerable<TEntity>(expression);
            }

            var genericParams = t.GetGenericArguments();
            var genericParam = genericParams[0];
            var enumerableType = typeof(AsyncEnumerable<>).MakeGenericType(genericParam);

            return (IQueryable)Activator.CreateInstance(enumerableType, expression);
        }

        public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
        {
            return new AsyncEnumerable<TElement>(expression);
        }

        public object Execute(Expression expression)
        {
            return _inner.Execute(expression);
        }

        public TResult Execute<TResult>(Expression expression)
        {
            return _inner.Execute<TResult>(expression);
        }

        public Task<object> ExecuteAsync(Expression expression, CancellationToken cancellationToken)
        {
            return Task.FromResult(Execute(expression));
        }

        public Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
        {
            return Task.FromResult(Execute<TResult>(expression));
        }
    }

    internal class AsyncEnumerator<T> : IDbAsyncEnumerator<T>
    {
        private readonly IEnumerator<T> _inner;

        public AsyncEnumerator(IEnumerator<T> inner)
        {
            _inner = inner;
        }

        public void Dispose()
        {
            _inner.Dispose();
        }

        public Task<bool> MoveNextAsync(CancellationToken cancellationToken)
        {
            return Task.FromResult(_inner.MoveNext());
        }

        public T Current => _inner.Current;

        object IDbAsyncEnumerator.Current => Current;
    }
}
like image 110
gaiazov Avatar answered Oct 22 '22 16:10

gaiazov