DapperLib / DapperLib/Dapper

Proposal: Add Support for Loosely Coupled Custom Query Parameter

Open
#1,798 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

With the ICustomQueryParameter we are able to create custom datatypes and control how the ADO.Net parameter is created and configured, this allows for a seamless coding experience.

I would like to expand on this functionality a little and add support for a loosely coupled custom query parameter. For this to work Dapper would use a Duck Typing technique and look for a method with the following exact instance level signature and use that to add the ADO.Net Parameter:

  • AddParameter(IDbCommand, string)

If the existing ICustomQueryParameter interface is implemented this will take priority.
The AddParameter method can be public, internal or private, this will allow the assembly developer to expose only the type functionality they are working on and support Dapper.

The motivation for a loosely coupled custom query parameter is to be able to support dapper with custom types without having to reference Dapper, dragging it along in projects that don't need it. Some projects have no Data Store requirement.

The following is what i have in mind, this would go immediately after the ICustomQueryParameter code:

var addParameterMethod = prop.PropertyType.GetMethod(nameof(ICustomQueryParameter.AddParameter), BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic, Type.DefaultBinder, new[] { typeof(IDbCommand), typeof(string) }, null);
if (addParameterMethod is not null)
{
	il.Emit(OpCodes.Ldloc, typedParameterLocal); // stack is now [parameters] [typed-param]
	il.Emit(callOpCode, prop.GetGetMethod()); // stack is [parameters] [custom]
	il.Emit(OpCodes.Ldarg_0); // stack is now [parameters] [custom] [command]
	il.Emit(OpCodes.Ldstr, prop.Name); // stack is now [parameters] [custom] [command] [name]
	il.EmitCall(OpCodes.Callvirt, addParameterMethod, null); // stack is now [parameters]
	continue;
}

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

Start by reading the existing ICustomQueryParameter handling and the code that emits its AddParameter call. Evaluate the proposed reflection and IL path, including method visibility and interface precedence; done means loosely coupled custom parameter types work without referencing Dapper while existing interface implementations remain supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.