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

Overload for Add method in DynamicParameters #820

Open
rafalkwol opened this issue Jul 25, 2017 · 0 comments
Open

Overload for Add method in DynamicParameters #820

rafalkwol opened this issue Jul 25, 2017 · 0 comments

Comments

@rafalkwol
Copy link

Hello,

I'm writing a custom library for LINQ-to-SQL translation and I'm using Dapper as a base for this project. The issue I have is something I can't seem to overcome while not using some "hacky" methods. In some part of my code I'm using GetValue(object value) method of FieldInfo/PropertyInfo class. I use it to get a value of part of the Expression provided to the method.

Sometimes (obviously) some of the parameters provided can resolve to "null" which is fine. But there is a problem when I'm trying to pass it as a parameter to Dapper. I do it by dynamically generating parameter names (eg. param_0, param_1 etc.) and that part is working OK. Then I add those parameters with the values I got from method mentioned earlier to the DynamicParameters class. Problem is that the DynamicParameters method Add(string name, object value = null, DbType? dbType = null, ParameterDirection? direction = null, int? size = null, byte? precision = null, byte? scale = null) is getting from me only the first two parameters. Which means that if "value" is equal to "null" than it can't resolve it properly on the database side due to unknown type. It only happens in some cases - an example could be if there WHERE clause of SQL query looks something like this: "@param_0 IS NULL".

I realize that there is a third parameter which is "dbType" and providing this one would solve the issue. The thing is that this kind of setup would work if I had compile time anonymous class in form of new { param_id = (int?)null }. So I find it a bit inconsistent that Dapper can infer type from anonymous type property, but there isn't an option to pass "Type" to some "Add" method overload in DynamicParameters. Therefore I would like to ask if there is a possibility that such overload would be added in the future? I could overcome this issue by mapping the DbType's myself, but it seems like than I would be missing one of the benefits of using Dapper as a base, which is automatic DbType mapping.

From what I checked in the source code I could possibly use "SqlMapper.LookupDbType" method to find DbType for each Type, but I don't think I should use this method as it's marked as Obsolete and should be only used for internal calls.

Best regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants