DapperLib / DapperLib/Dapper

Overload for Add method in DynamicParameters

Open
#820 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue points to DynamicParameters.Add and SqlMapper.LookupDbType; read those implementations first to understand existing type inference and null handling. The requested change is an overload that accepts type information for null values, with completion demonstrated by typed-null parameters resolving correctly without caller-maintained DbType mappings.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
backend, databases
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.