henkmollema / Dommel

CRUD operations with Dapper made simple.
MIT License
634 stars 100 forks source link

Cancellation Token #246

Closed DanieleSky closed 3 years ago

DanieleSky commented 3 years ago

Hi, I'm using your extension with Dapper and I would to know how I can cancel an expensive query with async methods (ex GetAllAsync). I see that all Async method don't have the CancellationToken parameter.

How I can do this?

Thanks

henkmollema commented 3 years ago

The underlying methods of Dapper which Dommel uses do not allow to pass a cancellation token. Therefore there is no point in Dommel adding them to its methods.

DanieleSky commented 3 years ago

Hi @henkmollema, I found that with CommandDefinitions you can pass a cancellation token. This is the reference https://stackoverflow.com/a/25545312/4189041

I tried this method and works.

For example, I think that the method GetAllAsync could become:

public static Task> GetAllAsync(this IDbConnection connection, IDbTransaction? transaction = null, CancellationTokenSource? tokenSource = null) where TEntity : class
{
  var sql = BuildGetAllQuery(connection, typeof(TEntity));
  LogQuery(sql);
  return connection.QueryAsync(new CommandDefinition(sql, transaction: transaction, cancellationToken: tokenSource.Token));
}

What do you think?

DanieleSky commented 3 years ago

Hi @henkmollema , any news about my last comment? Thank you