Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect Unbuffered QueryAsync implementation #76

Open
walidmabdelal opened this issue Jan 18, 2024 · 4 comments
Open

Incorrect Unbuffered QueryAsync implementation #76

walidmabdelal opened this issue Jan 18, 2024 · 4 comments
Assignees

Comments

@walidmabdelal
Copy link

Dapper now has the QueryUnbufferedAsync method to take care of this. The use of the QueryAsync with the unbuffered flag set to false as shown in the Buffered\Unbuffered page won't work as the SQL Mapper doesn't expose such a method, at least as of latest releases.

@JonathanMagnan JonathanMagnan self-assigned this Jan 18, 2024
@JonathanMagnan
Copy link
Member

Thank you for reporting, I will try to look at it over the weekend to update our documentation correctly.

@JonathanMagnan
Copy link
Member

Personal note:

  • QueryUnbufferedAsync seems to be added in v2.0.138. (On DbConnection only as explained by NickCraver here)
  • More info why here and here

@JonathanMagnan
Copy link
Member

Hello @walidmabdelal ,

Thank you again for letting us know.

We added a note in the async section here and a new page about Dapper QueryUnbufferedAsync

I believe this page is good enough until the next major version of Dapper is released.

Best Regards,

Jon

@walidmabdelal
Copy link
Author

Hi @JonathanMagnan ,

Happy to help.

Love the note at the end of the async page!

The QueryUnbufferedAsync is good to go; however here are some ideas for the future:

1- Coupling the QueryUnbufferedAsync with the robust IAsyncEnumerable type for maximum control and support for cancellation, a method signature could return IAsyncEnumerable

2- Cancellation: Enabling cancellation could look something like:

public async IAsyncEnumerable<T> QueryStreamAsync<T>(
     DbConnection connection,
     string commandText,
     CommandType commandType,
     object? parameters = null,
     int? commandTimeout = null,
     [EnumeratorCancellation] CancellationToken cancellationToken = default)
     where T : IEntity
 {
     IAsyncEnumerable<T> resultStream = await connection.QueryUnbufferedAsync<T>(commandText, parameters, transaction:null, commandTimeout, commandType);

     await foreach (var item in resultStream.WithCancellation(cancellationToken).ConfigureAwait(false))
     {
         yield return item;
     }
 }

Although in ASP.NET there is no SynchronizationContext so ConfigureAwait here is mroe of a convention and for behavior consistency.

Also, note the EnumeratorCancellation attribute annotation from the C# 8~12 features to tell the compiler to use the token passed to the method.

Thanks for the updates and your efforts Jon.

Gratefully,
Walid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants