Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeHandler should be supported for enum #286

Closed
wants to merge 1 commit into from
Closed

TypeHandler should be supported for enum #286

wants to merge 1 commit into from

Conversation

ShamsulAmry
Copy link

In the database, I'm storing single character status codes, and an enum to match the status codes. However, since the status codes are not integer and not direct string match against the enum I created, I implemented a TypeHandler to do custom deserialization from the status code to the enum. Then I found out Dapper will not call the TypeHandler for enum types, so here's the fix.

Basically I moved the TypeHandler cache check to the beginning of the block even before enum value processing because I believe user-defined TypeHandlers should come first before anything else, no?

… priority before anything else because it's user-defined
@vdaron
Copy link

vdaron commented May 18, 2015

Related to #259

@ShamsulAmry
Copy link
Author

Related to #258 too

@drusellers
Copy link

👍 want

@NickCraver
Copy link
Member

I believe this is now properly handled, see current code here:

if (hasTypeHandler)
{
#pragma warning disable 618
    il.EmitCall(OpCodes.Call, typeof(TypeHandlerCache<>).MakeGenericType(unboxType).GetMethod("Parse"), null); // stack is now [target][target][typed-value]
#pragma warning restore 618
}
else
{
    il.Emit(OpCodes.Unbox_Any, unboxType); // stack is now [target][target][typed-value]
}

If I'm missing something though, comment here and I'm happy to open the issue back up.

@NickCraver NickCraver closed this Oct 12, 2015
@vdaron
Copy link

vdaron commented Oct 12, 2015

Hi, current master branch doesn't work for me. The LookupDbType will not return the ITypeHandler if one is defined for an Enum and the deserializer IL generated code seems to ignore ITypeHandler for Enums.

This sample ITypeHandler is ignored for example :

public enum TestEnum
{
   One,Two,Three
}

public class EnumAsIntTypeHandler<T> : ITypeHandler
{
   public void SetValue(IDbDataParameter parameter, object value)
   {
      parameter.DbType = DbType.Int32;
      parameter.Value = (int)value;
   }
   public object Parse(Type destinationType, object value)
   {
      return (T)value;
   }
}

...
SqlMapper.AddTypeHandler(typeof(TestEnum), new EnumAsIntTypeHandler<TestEnum>());
...

Thanks for feedback @NickCraver !

Edit:I'm perfectly aware that my ITypeHandler sample is the default behavior for Enums :-)

@floyd-may
Copy link

+1, same behavior experienced as @vdaron. Need to be able to map letter codes to enums and back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants