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

Added CommandDefinition overload #2052

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions Dapper/SqlMapper.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,7 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TFirst, TSecond, T
if (wasClosed) cnn.Close();
}
}


/// <summary>
/// Perform an asynchronous multi-mapping query with an arbitrary number of input types.
Expand All @@ -965,11 +966,24 @@ private static async Task<IEnumerable<TReturn>> MultiMapAsync<TFirst, TSecond, T
/// <param name="commandType">Is it a stored proc or a batch?</param>
/// <returns>An enumerable of <typeparamref name="TReturn"/>.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Grandfathered")]
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)
{
var command = new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default);
return MultiMapAsync(cnn, command, types, map, splitOn);
}
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) =>
MultiMapAsync(cnn,
new CommandDefinition(sql, param, transaction, commandTimeout, commandType, buffered ? CommandFlags.Buffered : CommandFlags.None, default), types, map, splitOn);

/// <summary>
/// Perform an asynchronous multi-mapping query with an arbitrary number of input types.
/// This returns a single type, combined from the raw types via <paramref name="map"/>.
/// </summary>
/// <typeparam name="TReturn">The combined type to return.</typeparam>
/// <param name="cnn">The connection to query on.</param>
/// <param name="types">Array of types in the recordset.</param>
/// <param name="splitOn">The field we should split and read the second object from (default: "Id").</param>
/// <param name="command">The command to execute.</param>
/// <param name="map">The function to map row types to the return type.</param>
/// <returns>An enumerable of <typeparamref name="TReturn"/>.</returns>
[System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Grandfathered")]
public static Task<IEnumerable<TReturn>> QueryAsync<TReturn>(this IDbConnection cnn, CommandDefinition command, Type[] types, Func<object[], TReturn> map, string splitOn = "Id") =>
MultiMapAsync(cnn, command, types, map, splitOn);

private static async Task<IEnumerable<TReturn>> MultiMapAsync<TReturn>(this IDbConnection cnn, CommandDefinition command, Type[] types, Func<object[], TReturn> map, string splitOn)
{
Expand Down
68 changes: 38 additions & 30 deletions tests/Dapper.Tests/AsyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -866,38 +866,46 @@ public async Task TestMultiMapArbitraryMapsAsync()
return board;
};

Action<ReviewBoard?> assertResult = p =>
{
Assert.Equal(1, p.Id);
Assert.Equal("Review Board 1", p.Name);
Assert.NotNull(p.User1);
Assert.NotNull(p.User2);
Assert.NotNull(p.User3);
Assert.NotNull(p.User4);
Assert.NotNull(p.User5);
Assert.NotNull(p.User6);
Assert.NotNull(p.User7);
Assert.NotNull(p.User8);
Assert.NotNull(p.User9);
Assert.Equal(1, p.User1.Id);
Assert.Equal(2, p.User2.Id);
Assert.Equal(3, p.User3.Id);
Assert.Equal(4, p.User4.Id);
Assert.Equal(5, p.User5.Id);
Assert.Equal(6, p.User6.Id);
Assert.Equal(7, p.User7.Id);
Assert.Equal(8, p.User8.Id);
Assert.Equal(9, p.User9.Id);
Assert.Equal("User 1", p.User1.Name);
Assert.Equal("User 2", p.User2.Name);
Assert.Equal("User 3", p.User3.Name);
Assert.Equal("User 4", p.User4.Name);
Assert.Equal("User 5", p.User5.Name);
Assert.Equal("User 6", p.User6.Name);
Assert.Equal("User 7", p.User7.Name);
Assert.Equal("User 8", p.User8.Name);
Assert.Equal("User 9", p.User9.Name);
};

var data = (await connection.QueryAsync<ReviewBoard>(sql, types, mapper).ConfigureAwait(false)).ToList();

var p = data[0];
Assert.Equal(1, p.Id);
Assert.Equal("Review Board 1", p.Name);
Assert.NotNull(p.User1);
Assert.NotNull(p.User2);
Assert.NotNull(p.User3);
Assert.NotNull(p.User4);
Assert.NotNull(p.User5);
Assert.NotNull(p.User6);
Assert.NotNull(p.User7);
Assert.NotNull(p.User8);
Assert.NotNull(p.User9);
Assert.Equal(1, p.User1.Id);
Assert.Equal(2, p.User2.Id);
Assert.Equal(3, p.User3.Id);
Assert.Equal(4, p.User4.Id);
Assert.Equal(5, p.User5.Id);
Assert.Equal(6, p.User6.Id);
Assert.Equal(7, p.User7.Id);
Assert.Equal(8, p.User8.Id);
Assert.Equal(9, p.User9.Id);
Assert.Equal("User 1", p.User1.Name);
Assert.Equal("User 2", p.User2.Name);
Assert.Equal("User 3", p.User3.Name);
Assert.Equal("User 4", p.User4.Name);
Assert.Equal("User 5", p.User5.Name);
Assert.Equal("User 6", p.User6.Name);
Assert.Equal("User 7", p.User7.Name);
Assert.Equal("User 8", p.User8.Name);
Assert.Equal("User 9", p.User9.Name);
assertResult( data[0] );

data = (await connection.QueryAsync<ReviewBoard>(new CommandDefinition(sql, flags: CommandFlags.None), types, mapper).ConfigureAwait(false)).ToList();

assertResult( data[0] );
}
finally
{
Expand Down