SetValue is not invoked in a Dapper custom TypeHandler
As per THIS question:
Given:
public class GuidHandler : SqlMapper.TypeHandler<Guid>
{
public override void SetValue(IDbDataParameter parameter, Guid value)
{
// My Custom logic
}
public override Guid Parse(object value)
{
return Guid.Parse(value.ToString());
}
}
...
SqlMapper.AddTypeHandler(new GuidHandler());
Even though the Parse is correctly invoked when reading from the DB, the SetValue however never gets called when writing the data.
Any idea of what is going on?
Dapper doesn't support custom type handlers for in-built primitive types at the moment, see #303 #206 and #458
Seems this issue has fallen off the radar. Is there a workaround?
Not at the moment, the main reason is it could be a breaking change for existing code as the simple solution is to move the check for ITypeHandler earlier in the code.
Another solution would be to have 2 functions to register a type handler, a "late" function (the current one) and an "early" function. The issue with that is it makes the API confusing as you have to explain the difference.
I guess one could add a configuration item like "UseLegacyTypeHandling" which is turned on by default to maintain backwards compatibility.
This bug has been living for 6 years so far. 6 years of broken type mapping, just not to break compatibility. Are there any plans to fix it? Maybe workarounds?