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

Nullability on generic typed FirstOrDefault/SingleOrDefault methods #19

Open
j-caetano opened this issue Oct 17, 2024 · 1 comment
Open

Comments

@j-caetano
Copy link

Hey, found an issue regarding the null safety on return type of OrDefault methods on IDapperSqlBuilderExtensions as these methods can return null.

The FirstOrDefault/SingleOrDefault extension methods return non-nullable types while using the Dapper methods that return a nullable generic type.

Dapper:

public static Task<T?> QueryFirstOrDefaultAsync<T>(this IDbConnection cnn, string sql, object? param = null, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null) =>
    QueryRowAsync<T?>(cnn, Row.FirstOrDefault, typeof(T), new CommandDefinition(sql, param, transaction, commandTimeout, commandType, CommandFlags.None, default))

InterpolatedSql.Dapper: IDapperSqlBuilderExtensions.cs

public static T QueryFirstOrDefault<T>(this IDapperSqlBuilder builder, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null)
{
    var command = builder.Build();
    return command.DbConnection.QueryFirstOrDefault<T>(sql: command.Sql, param: ParametersDictionary.LoadFrom(command), transaction: transaction, commandTimeout: commandTimeout, commandType: commandType);
}

Adding a nullable return type would solve the issue

public static T? QueryFirstOrDefault<T>(this IDapperSqlBuilder builder, IDbTransaction? transaction = null, int? commandTimeout = null, CommandType? commandType = null)
{
    var command = builder.Build();
    return command.DbConnection.QueryFirstOrDefault<T>(sql: command.Sql, param: ParametersDictionary.LoadFrom(command), transaction: transaction, commandTimeout: commandTimeout, commandType: commandType);
}
@Drizin
Copy link
Owner

Drizin commented Oct 19, 2024

Would you like to submit a PR?

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

2 participants