Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

Mapping to tuple hides type incompatibilities

Open kristofturanszki opened this issue 1 year ago • 0 comments

Hi, I was wondering if this is an intentional feature or a bug.

Correct behavior I think: If one uses a designated type (for example a record) to map the result set and there is a type difference between the desired destination's field type and the result set, it throws a runtime error as expected.

Questionable behavior: In case one uses a tuple for mapping a result set of a query/stored procedure, type inconsistencies are hidden, not like the first case.

Example code:

[Fact]
public async Task Independent()
{
    await using var connection = new SqlConnection("Data Source=localhost;Initial Catalog=test;TrustServerCertificate=True;Trusted_Connection=True");

    await connection.OpenAsync();

    //Tuple - No exception
    var result1 = await connection.QueryAsync<(long IntegerColumn, int BigintColumn)>("SELECT IntegerColumn, BigintColumn FROM dbo.TestTable");

    //With record - Correct mapping - No exception
    var result3 = await connection.QueryAsync<TestResultCorrect>("SELECT IntegerColumn, BigintColumn FROM dbo.TestTable");

    //With record - Incorrect mapping - Exception is thrown: A parameterless default constructor or one matching signature...
    var result2 = await connection.QueryAsync<TestResultIncorrect>("SELECT IntegerColumn, BigintColumn FROM dbo.TestTable");
}

private record TestResultIncorrect(long IntegerColumn, int BigintColumn);
private record TestResultCorrect(int IntegerColumn, long BigintColumn);

Datatable:

create table dbo.TestTable
(
    IntegerColumn int,
    BigintColumn  bigint
)
go

kristofturanszki avatar Aug 16 '24 15:08 kristofturanszki