Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

SetValue is not invoked in a Dapper custom TypeHandler

Open oS-So opened this issue 9 years ago • 5 comments

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?

oS-So avatar Sep 10 '16 23:09 oS-So

Dapper doesn't support custom type handlers for in-built primitive types at the moment, see #303 #206 and #458

chilversc avatar Sep 19 '16 20:09 chilversc

Seems this issue has fallen off the radar. Is there a workaround?

kwaclaw avatar Sep 22 '16 18:09 kwaclaw

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.

chilversc avatar Sep 23 '16 15:09 chilversc

I guess one could add a configuration item like "UseLegacyTypeHandling" which is turned on by default to maintain backwards compatibility.

kwaclaw avatar Sep 29 '16 15:09 kwaclaw

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?

andrey-perepelitsyn avatar May 25 '22 07:05 andrey-perepelitsyn