Feature Request: Support for Open Generic TypeHandler registration
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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