Suggestion for mapping non-standard Enums
Nobody has claimed this yet.
- Dominant language
- C#
- Stars
- 18.4k
- Forks
- 3.7k
- Avg merge
- 5h 8m
- Merged PRs (30d)
- 1
Description
I had posted issue #448 in Feb-2016 where we had implemented our own method for parsing enums; when we upgraded from VS 2013 to VS 2015 (Roslyn), our custom enum parsing implementation failed (see issue #448). We do have a work-a-round in our current code base. With the other issues related to Enum parsing (#259, #458) and the planned support for V2 with issue #688, I thought I'd suggest a fairly non-invasive method for allowing a custom implementation of mapping Enums.
Note: I had originally forked the repo and tried to create a pull request, but I could not get VS 2015 to compile w/o errors before any changes.
Suggested Change to Support Enum Mapping
SqlMapper.Settings
// add new property
public static Func<Type, string, bool, object> EnumParse { get; set; }
public static void SetDefaults()
{
CommandTimeout = null;
ApplyNullValues = false;
// set up default value
EnumParse = Enum.Parse;
}
SqlMapper
// delete the enumParse field and update the usage
// to consume the SqlMapper.Settings.EnumParse property:
il.EmitCall(OpCodes.Call, SqlMapper.Settings.EnumParse.Method, null)
Usage
To override the default Enum.Parse method, set the SqlMapper.Settings.EnumParse property prior to any dapper calls:
Func<Type, string, bool, object> enumParse = ( type, value, flag ) => ToEnum( type, value );
SqlMapper.Settings.EnumParse = enumParse;
private static object ToEnum( Type type, string value )
{
// evaluate and return the result
return result;
}
Would something like this be an option for the dapper team to consider?
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 with SqlMapper.Settings and the enum parsing usage in SqlMapper, then review the related enum issues #259, #448, and #458 alongside planned V2 work in #688. Done means a configurable enum parser can replace the default Enum.Parse behavior without breaking existing mapping.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- database
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100