DapperLib / DapperLib/Dapper

Feature Request: Support for Open Generic TypeHandler registration

Open
#2,190 1 comment 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

Currently, Dapper does not support registering open generic types for TypeHandler.
This creates a significant maintenance burden and forces developers to violate the DRY principle.

I have a generic TypeHandler that works perfectly for any T, for example, handling HashSet for PostgreSQL arrays:

public class HashSetHandler<T> : SqlMapper.TypeHandler<HashSet<T>>
{
    public override void SetValue(IDbDataParameter parameter, HashSet<T> value)
    {
        // Solves Npgsql issue with IEnumerable parameters
        parameter.Value = value?.ToArray(); 
    }

    public override HashSet<T> Parse(object value)
    {
        if (value is T[] array)
        {
            return new HashSet<T>(array);
        }
        return new HashSet<T>();
    }
}

To use this handler, I am forced to manually register every single closed generic type variation used in the application. This is tedious, error-prone, and clutters the startup configuration:

SqlMapper.AddTypeHandler(new HashSetHandler<Guid>());
SqlMapper.AddTypeHandler(new HashSetHandler<int>());
SqlMapper.AddTypeHandler(new HashSetHandler<string>());
SqlMapper.AddTypeHandler(new HashSetHandler<MyEnum>());
// ... repeated for every new type added to the domain models

Please allow the registration of open generic handlers. Dapper should be able to resolve the concrete handler at runtime if an open generic definition is registered.

Desired API:

SqlMapper.AddTypeHandler(typeof(HashSet<>), typeof(HashSetHandler<>));

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 at SqlMapper.AddTypeHandler and the existing TypeHandler registration and resolution paths. Trace how closed generic handlers are currently registered and selected, then review the requested open-generic API and define tests for resolving handlers such as HashSet for multiple concrete T types. Done means the API supports open generic registration without requiring each closed type to be added manually.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
backend-api-design, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.