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

[API Proposal]: Add Async Methods to IDataReader #81876

Open
RAAvenger opened this issue Feb 9, 2023 · 3 comments
Open

[API Proposal]: Add Async Methods to IDataReader #81876

RAAvenger opened this issue Feb 9, 2023 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading.Tasks needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Milestone

Comments

@RAAvenger
Copy link

Background and motivation

I'm writing a code that exposes an NpgsqlDataReader. I would like to use async methods of DataReader and also be able to test my code by mocking DataReader, so i prefer to expose an IDataReader that can be mocked but if i do this i can't use async methods. Of course there is workaround like writing wrapper and ... but it would be great if the interface contains async methods.

API Proposal

namespace System.Data
{
    public interface IDataReader : IDisposable, IDataRecord
    {
        int Depth { get; }
        bool IsClosed { get; }
        int RecordsAffected { get; }
        void Close();
        Task CloseAsync();
        DataTable? GetSchemaTable();
        Task<DataTable?> GetSchemaTableAsync(CancellationToken cancellationToken);
        bool NextResult();
        Task<bool> NextResultAsync(CancellationToken cancellationToken);
        bool Read();
        Task<bool> ReadAsync(CancellationToken cancellationToken);
    }
}

API Usage

var connection = new NpgsqlConnection(connectionString);
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
var command = connection.CreateCommand();
command.CommandText = query;
IDataReader reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);
while(await reader.ReadAsync(connectionString).ConfigureAwait(false))
{
// get data
}

Alternative Designs

No response

Risks

No response

@RAAvenger RAAvenger added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Feb 9, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Feb 9, 2023
@ghost
Copy link

ghost commented Feb 9, 2023

Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

I'm writing a code that exposes an NpgsqlDataReader. I would like to use async methods of DataReader and also be able to test my code by mocking DataReader, so i prefer to expose an IDataReader that can be mocked but if i do this i can't use async methods. Of course there is workaround like writing wrapper and ... but it would be great if the interface contains async methods.

API Proposal

namespace System.Data
{
    public interface IDataReader : IDisposable, IDataRecord
    {
        int Depth { get; }
        bool IsClosed { get; }
        int RecordsAffected { get; }
        void Close();
        Task CloseAsync();
        DataTable? GetSchemaTable();
        Task<DataTable?> GetSchemaTableAsync(CancellationToken cancellationToken);
        bool NextResult();
        Task<bool> NextResultAsync(CancellationToken cancellationToken);
        bool Read();
        Task<bool> ReadAsync(CancellationToken cancellationToken);
    }
}

API Usage

var connection = new NpgsqlConnection(connectionString);
await connection.OpenAsync(cancellationToken).ConfigureAwait(false);
var command = connection.CreateCommand();
command.CommandText = query;
IDataReader reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);
while(await reader.ReadAsync(connectionString).ConfigureAwait(false))
{
// get data
}

Alternative Designs

No response

Risks

No response

Author: RAAvenger
Assignees: -
Labels:

api-suggestion, area-System.Threading.Tasks

Milestone: -

@svick
Copy link
Contributor

svick commented Feb 9, 2023

Just adding methods to an interface is a massive breaking change, so it's not going to happen.

Adding them as default interface methods is much less of a breaking change, but then the question becomes what the default implementation should be and I'm not sure there is a good answer to that.

Can't you mock DbDataReader for your tests instead?

@RAAvenger
Copy link
Author

thanks for your suggestion. using DbDataReader will resolve my issue.
A non-breaking option that comes to mind is to define a new Interface (e.g. IAsyncDataReader) for all those async method overloads of DbDataReader class and let DbDataReader implement the aforementioned interface alongside the existing IDataReader

@tannergooding tannergooding added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed untriaged New issue has not been triaged by the area owner labels Jun 24, 2024
@stephentoub stephentoub added this to the Future milestone Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading.Tasks needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Projects
None yet
Development

No branches or pull requests

4 participants