Let's say I have a Projects
table in my DB with multiple relationships. Various different queries in the DB layer of my c# code would be interested in joining this table with different others. So I abstracted away the following:
public IQueryable<Project> QueryProject(Project prj)
{
return Context.Projects.Where(p => p.id == prj.id);
}
... so I can reuse this method in various other scenarios, such as...
_queryService.QueryProject(prj).Include(p => p.Users)
... or
_queryService.QueryProject(prj).Include(p => p.Artefacts)
... etc. At one point, I decide that making action methods asynchronous is a good idea, so I try the following:
await _queryService.QueryProject(prj).Include(p => p.Artefacts).SingleAsync();
However, the moment I try to make any operation using the QueryProject
method asynchronous, I get the following error from my unit tests:
The source IQueryable doesn't implement IAsyncEnumerable. Only sources that implement IAsyncEnumerable can be used for Entity Framework asynchronous operations.
So clearly I shouldn't be using IQueryable as a return type for my first QueryProject
abstraction. However, I'm struggling to find a viable alternative. I do NOT want any DB retrieval of entities during the execution of QueryProject
. I'm also not interested in hacks that simply hide this error without bringing me any benefit. I'm only trying to abstract away repeating code.
Is some type of alternative supported in .NET core 2.1?
P.S. Ivan Stoev raised a valid concern, that the problem could be in my unit tests. Here's my mocking code for reference (using Moq):
mockServices.Setup(x => x.QueryProject(It.IsAny<SomeProjectAbstraction>()))
.Returns(new List<Project>
{
new Project
{
Name = ...
}
}.AsQueryable());