Link Search Menu Expand Document

RepoDB's Complete Support to CancellationToken

Oct 3, 2020

Today, we are announcing the complete support of RepoDB to the CancellationToken.

Starting at version v1.12.4 and its corresponding extended libraries for different DB Providers at version 1.1.1, all the Async methods within the library will be having an additional argument cancellationToken (defaulted to CancellationToken.None and/or default).

How will it affect you?

When you upgrade, you can leverage the possibility of cancellable async operations. All the existing calls you made to any operations will not be affected (both compilation/signatures and the behaviors).

See the sample calls below for ExecuteNonQuery.

var tokenSource = new CancellationTokenSource();
using (var connection = new SqlConnection(connectionString))
{
    var result = await connection.ExecuteQueryAsync<DateTime>("SELECT GETUTCDATE();",
        cancellationToken: tokenSource.Token);
    // You can cancel the token anytime
}

And for the Query operation.

var tokenSource = new CancellationTokenSource();
using (var connection = new SqlConnection(connectionString))
{
    var people = await connection.QueryAllAsync<Person>(cancellationToken: tokenSource.Token);
    // You can cancel the token anytime
}

And for the Insert operation.

var tokenSource = new CancellationTokenSource();
using (var connection = new SqlConnection(connectionString))
{
    var id = await connection.InsertAsync<Person, long>(new
    {
        Name = "John Doe",
        Address = "New York"
    },
    cancellationToken: tokenSource.Token);
    // You can cancel the token anytime
}

Such calls will be identical to all other async methods/operations within the library.

Breaking Changes

There is one breaking change that will be covered by this release. But the use-case to this is very minimal and/or almost 0.

If you have created your own customized IDbHelper object, then, you need to change your signature by simply adding the cancellationToken argument in both methods defined on this interface.

GetFieldsAsync

public async Task<IEnumerable<DbField>> GetFieldsAsync(IDbConnection connection,
    string tableName,
    IDbTransaction transaction = null,
    CancellationToken cancellationToken = default)

GetScopeIdentityAsync

public async Task<object> GetScopeIdentityAsync(IDbConnection connection,
    IDbTransaction transaction = null,
    CancellationToken cancellationToken = default)

Closing Note

You can visit the actual release from our Github page.