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

QueryAsync with custom mapping AND CancellationToken #2125

Open
HrvojeJuric opened this issue Oct 22, 2024 · 7 comments
Open

QueryAsync with custom mapping AND CancellationToken #2125

HrvojeJuric opened this issue Oct 22, 2024 · 7 comments

Comments

@HrvojeJuric
Copy link

I want to use QueryAsync with multiple types mapped onto a single return type WITH a cancellation token but none of the QueryAsync functions allow for that.

My current solution is to use the private MultiMapAsync method but this is not ideal and I don't know what the right solution to this would be but I'd like to talk about it and if you're down I can make a pull request.

@goerch
Copy link
Contributor

goerch commented Oct 28, 2024

Maybe relevant?

@haras-unicorn
Copy link

no because that one doesnt do multi mapping

@goerch
Copy link
Contributor

goerch commented Oct 29, 2024

I think we use multi mapping in a similar way. Let me see

    protected CommandDefinition CompileQuery(Query query, IDbTransaction transaction, 
        CancellationToken token, CommandFlags flags = CommandFlags.Buffered)
    {
        var statement = _compiler.Compile(query).ToString();
        return new CommandDefinition(statement, transaction:transaction, 
            cancellationToken:token, flags:flags);
    } 

    protected async Task<IEnumerable<TEntity>> QueryAsync<TFirst, TSecond>(Query query, IDbTransaction transaction, CancellationToken token,
        Func<TFirst, TSecond, TEntity> map, string splitOn = "id")
        where TFirst: new() where TSecond: new()
    {
        var command = CompileQuery(query, transaction, token, CommandFlags.Buffered);
        return await transaction.Connection!.QueryAsync<TFirst, TSecond>(command, map, splitOn).ConfigureAwait(false);
    }

But I might be missing something.

@HrvojeJuric
Copy link
Author

this is for a static number of objects in the result
i want dynamic number of objects in the result and cancellation token

@goerch
Copy link
Contributor

goerch commented Oct 29, 2024

i want dynamic number of objects in the result and cancellation token

It could be helpful if you point us to the signature which is missing the CommandDefinition parameter?

@HrvojeJuric
Copy link
Author

HrvojeJuric commented Oct 29, 2024

public static Task<IEnumerable<TReturn>> QueryAsync<TReturn>(this IDbConnection cnn, string sql, Type[] types, Func<object[], TReturn> map, object? param = null, IDbTransaction? transaction = null, bool buffered = true, string splitOn = "Id", int? commandTimeout = null, CommandType? commandType = null)

this one

i want this to be public because ^^ just wraps all the parameters into a command definition and calls this:

private static async Task<IEnumerable<TReturn>> MultiMapAsync<TReturn>(this IDbConnection cnn, CommandDefinition command, Type[] types, Func<object[], TReturn> map, string splitOn)

@itcotta
Copy link

itcotta commented Dec 19, 2024

Hi, I've ran into the same use case. Is there really no way of passing a CancellationToken when using custom mapping?

itcotta added a commit to itcotta/Dapper that referenced this issue Dec 30, 2024
This change introduces a CancellationToken parameter to the QueryAsync method, enabling support for cooperative cancellation of asynchronous database queries. The modification ensures better resilience and control over long-running or potentially cancelable operations.

This fixes issues DapperLib#2125 and DapperLib#1938.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants