-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
Background and motivation
I would like to use DbEnumerator.MoveNextAsync, which does not exist. (MoveNext does exist, but again, I want to read asynchronously.) DbEnumerator accepts IDataReader as a parameter, and since IDataReader does not support ReadAsync, DbEnumerator cannot be readily given async abilities without breaking changes.
I have made a DbDataReaderEnumerator class which requires DbDataReader as a parameter. DbDataReader has a ReadAsync method, so adding DbDataReaderEnumerator.MoveNextAsync is trivial. This class just reuses MoveNext code to make a MoveNextAsync with async enhancements.
This is my suggested file:
https://github.com/wessleym/runtime/blob/d49c266b80dcc36566e55b72d93da2fe1fc98509/src/libraries/System.Data.Common/src/System/Data/Common/DbDataReaderEnumerator.cs
API Proposal
namespace System.Data.Common;
public class DbDataReaderEnumerator : IEnumerator<DbDataRecord>, IAsyncEnumerator<DbDataRecord>
{
public DbDataReaderEnumerator(DbDataReader reader);
public DbDataRecord Current { get; }
public bool MoveNext();
public async ValueTask<bool> MoveNextAsync();
}API Usage
var readerEnumerator = new DbDataReaderEnumerator(reader);
var anyResults = await readerEnumerator.MoveNextAsync().ConfigureAwait(false);Alternative Designs
An alternative would be to allow DbEnumerator to accept DbDataReader as a parameter, but that would introduce breaking changes to callers who were passing an IDataReader as a parameter.
Risks
I am aware of no risks since DbDataReaderEnumerator is a new class.