-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
SqlMapper.Settings.cs: New option "StrictBinding" #2101
Comments
Hello, at a glance your issue looks like a legitimate concern. However, data type definitions on SQL Server and C# is relatable, but mutually exclusive. Therefore, the connection library can only take a stab at parsing the value to correct form. If you look at the implementation of the private static T Parse<T>(object? value)
{
if (value is null || value is DBNull) return default!;
if (value is T t) return t;
var type = typeof(T);
type = Nullable.GetUnderlyingType(type) ?? type;
if (type.IsEnum)
{
if (value is float || value is double || value is decimal)
{
value = Convert.ChangeType(value, Enum.GetUnderlyingType(type), CultureInfo.InvariantCulture);
}
return (T)Enum.ToObject(type, value);
}
if (typeHandlers.TryGetValue(type, out ITypeHandler? handler))
{
return (T)handler.Parse(type, value)!;
}
return (T)Convert.ChangeType(value, type, CultureInfo.InvariantCulture);
} |
That code shows me that it is relatively easy to implement:
The only thing still to do is then to define method |
Problem solved. Why don't you go ahead and implement this by yourself? This is the open-source world after all. Create a pull request and send it in. If you are able to implement it in a way that:
I am sure there won't be any issues and your request will be approved. |
I would like to see a new option
bool StrictBinding
or similar inSqlMapper.Settings.cs
that solves the following issues when set totrue
:Issue 1: Null assignments to non-nullable (value) types
Currently, the following applies:
which is not the behavior I was hoping for - I would have liked that Dapper slaps an exception in my face, saying "Are you OK? You are attempting to assign a
null
value to anInt32
!" Or less prosa:Reason: I made a mistake, my data type in code does not match the data type returned by my query and I should have used one of the following options instead:
or
Issue 2: Narrowing casts (e.g. Int64 -> Int32)
Currently, the following applies:
which is cool at first glance (why not casting it as it fits into the range) but no, again, I prefer Dapper would slap me an exception in my face, saying "Are you OK? You are attempting to assign an
Int64
value to anInt32
!" Or less prosa:Reason: I made a mistake, it is not safe to cast an
Int64
into anInt32
, that could be a sleeping bug that strikes when my code is long in production, e.g. when the record id exceedsInt32.MaxValue
. Again my data type in code does not match the data type returned by my query and I should have used one of the following options instead:or
What do you think?
The text was updated successfully, but these errors were encountered: