I'm developing an OData endpoint to return a list of items from a database.
I'm returning an IQueryable
and letting the front end handling the querying / filtering / expanding / paging through the odata query options.
Consider the following method:
[ODataRoute]
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public IActionResult Get() {
return Ok(context.Vendors.AsQueryable());
}
I understand that async
is not really needed, because the query is not being materialized here.
In this context, when is the query being materialized?
Is it being done in an asynchronous fashion?
You asked a few question Ill try to answer them all and clarify a few questions.
I understand that async is not really needed
Async is never really needed the question is will async benefit your application. E.g When a call is being made to the DbContext there is some network operations which take time, If you do not use async during that time a thread will be waiting for a response, instead of being return to the thread pool to handle other requests.
So yes, it's not needed but it could be useful.
In this context, when is the query being materialized?
The query is materialized (in the middleware) when the endpoint is accessed,
Is it being done in an asynchronous fashion?
No, When you pass the Queryable you are just deferring the execution until later this is still being done synchronously, you need to be using the async/await pattern for you to be doing something asynchronously