Automapper Enum Description Attribute

3.4k views Asked by At

I'm having difficulties mapping from an Enum Description Attribute. I've been looking all over for a useful example with very little luck. I know there are some other examples out there but I'm still struggling with this particular scenario.

Here is my Enum:

public enum ResolveCodeEnum
{
    [Description("Resolved - Workaround")]
    ResolvedWorkaround = 1,
    [Description("Resolved - Permanently")]
    ResolvedPermanently = 2,
    [Description("Resolved - Unknown")]
    ResolvedUnkown = 3,
    [Description("Cannot Reproduce")]
    CannotReproduce = 4,
    [Description("Invalid")]
    Invalid = 5,
    [Description("Cancelled")]
    Cancelled = 6
}

Here is my Enum Helper class:

public class EnumHelper
{
    public static string GetEnumDescription(Enum value)
    {
        FieldInfo fi = value.GetType().GetField(value.ToString());

        DescriptionAttribute[] attributes =
            (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

        if (attributes != null && attributes.Length > 0)
            return attributes[0].Description;
        else
            return value.ToString();
    }
}

My goal is to map source to destination via Enum Description Attribute

Here is what I have so far in my mapping configuration:

  Mapper.CreateMap<Result, Incident>()
            .ForMember(dest => dest.Status,
                opts => opts.MapFrom(src => src.state));

Here is the abbreviated Result class:

public class Result
{
    public string sys_id { get; set; }
    public string state { get; set; }
}

Here is the abbreviated Incident class:

public class Incident
{
    public string Id{ get; set; }
    public string Status{ get; set; }
}

Note: the state property of the Result class is a string

For example:

My goal is to get

 Incident.Status = "Resolved - Workaround"

From

 Result.state = "1"

I've been struggle with the automapper syntax to use with my EnumHelper class

If anybody can help me out on this, it would be greatly appreciated :)

1

There are 1 answers

3
Andrew Whitaker On BEST ANSWER

Two ways to do this:

  1. Inline with ResolveUsing:

        Mapper.CreateMap<Result, Incident>()
            .ForMember(
                dest => dest.Status,
                opt => opt.ResolveUsing(src =>
                {
                    var value = (ResolveCodeEnum)Enum.Parse(
                                    typeof(ResolveCodeEnum), src.state);
    
                    return EnumHelper.GetEnumDescription(value);
                }));
    
  2. With a custom ValueResolver:

    public class EnumValueResolver : ValueResolver<Result, string>
    {
        protected override string ResolveCore(Result src)
        {
            var value = (ResolveCodeEnum)Enum.Parse(typeof(ResolveCodeEnum), src.state);
    
            return EnumHelper.GetEnumDescription(value);    
        }
    }
    

    Usage:

    Mapper.CreateMap<Result, Incident>()
        .ForMember(
            dest => dest.Status,
            opt => opt.ResolveUsing<EnumValueResolver>());
    

I'd recommend #2 since it's much cleaner.