2

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 :)

jaypman
  • 167
  • 2
  • 11

1 Answers1

3

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.

Andrew Whitaker
  • 124,656
  • 32
  • 289
  • 307
  • I've tried both of these and still get this exception: "Can't resolve this to Queryable Expression" – BBauer42 Mar 30 '16 at 12:06
  • @BBauer42: That sounds like it might possibly be related to Entity Framework or another ORM. If that's the case it might be worth opening a new question. – Andrew Whitaker Mar 30 '16 at 13:33
  • Yeah, its EF and its during a queryable projection. I'm opening a question thanks. – BBauer42 Mar 31 '16 at 11:51