Home > Net >  AutoMapper - Map from object to enum
AutoMapper - Map from object to enum

Time:09-23

I have this class:

public class EntityClass
{
    public bool IsExempt { get; set; }
    public bool IsOverdue { get; set; }
    public SourceEnum Status { get; set; }
}

And these two enums:

public enum SourceEnum
{
    NotSet = 0,
    InProgress,
    Submitted,
    AssessmentComplete,
    Complete
}

[Flags]
public enum DestinationEnum
{
    None = 0,

    [Description("Exempt")]
    Exempt = 1,

    [Description("Unset")]
    Unset = 2,

    [Description("Overdue")]
    Overdue = 3,

    [Description("In Progress")]
    InProgress = 4,

    [Description("Submitted")]
    Submitted = 5,

    [Description("Conf. Pending")]
    ConfirmationPending = 6,

    [Description("Complete")]
    Completed = 7
}

I want to map from EntityClass to DestinationEnum, I tried it this way:

var config = new MapperConfiguration(cfg => cfg.CreateMap<EntityClass, DestinationEnum>()
                .ForMember(dest => dest, opt => opt.MapFrom(source => source.IsExempt ? DestinationEnum.Exempt : DestinationEnum.None))
                .ForMember(dest => dest, opt => opt.MapFrom(source => source.IsOverdue ? DestinationEnum.Overdue : DestinationEnum.None))

                .ForMember(dest => dest, opt => opt.MapFrom(source => source.Status == SourceEnum.InProgress ? DestinationEnum.InProgress : DestinationEnum.None))
                .ForMember(dest => dest, opt => opt.MapFrom(source => source.Status == SourceEnum.Complete ? DestinationEnum.Completed : DestinationEnum.None))
                .ForMember(dest => dest, opt => opt.MapFrom(source => source.Status == SourceEnum.AssessmentComplete ? DestinationEnum.ConfirmationPending : DestinationEnum.None))
                .ForMember(dest => dest, opt => opt.MapFrom(source => source.Status == SourceEnum.Submitted ? DestinationEnum.Submitted : DestinationEnum.None))
                .ForMember(dest => dest, opt => opt.MapFrom(source => source.Status == SourceEnum.NotSet ? DestinationEnum.Unset : DestinationEnum.None)));

var mapper = config.CreateMapper();

var entityClassObj = new EntityClass { IsExempt = true, IsOverdue = true, Status = SourceEnum.InProgress };

var result = mapper.Map<DestinationEnum>(entityClassObj);

When I run my code, it returns:

Custom configuration for members is only supported for top-level individual members on a type.

I tried the solution from here but unfortunately the IncludeMembers method is not available. I am using AutoMapper 6.0.2.

CodePudding user response:

As @Lucian mentioned, you need to implement the Custom Type Converter for your scenario.

Solution 1

public class EntityClassToDestinationEnumConverter : ITypeConverter<EntityClass, DestinationEnum>
{
    public DestinationEnum Convert(EntityClass src, DestinationEnum dest, ResolutionContext context)
    {
        if (src.IsExempt)
            return DestinationEnum.Exempt;
            
        if (src.IsOverdue)
            return DestinationEnum.Overdue;
            
        switch (src.Status)
        {
            case SourceEnum.InProgress:
                return DestinationEnum.InProgress;
            case SourceEnum.Complete:
                return DestinationEnum.Completed;
            case SourceEnum.AssessmentComplete:
                return DestinationEnum.ConfirmationPending;
            case SourceEnum.Submitted:
                return DestinationEnum.Submitted;
            case SourceEnum.NotSet:
                return DestinationEnum.Unset;
        }
            
        return DestinationEnum.None;
    }
}

Your Mapping Profile or MapperConfiguration to map from EntityClass to DestinationEnum.

MapperConfiguration _config = new MapperConfiguration(config => 
{
    config.CreateMap<EntityClass, DestinationEnum>()
        .ConvertUsing<EntityClassToDestinationEnumConverter>();
});

Solution 2

Or writing a method for the logic without a custom type resolver class.

public static class Helpers
{
    public static DestinationEnum ConvertFromEntityClassToDestinationEnum(EntityClass src)
    {
        if (src.IsExempt)
                return DestinationEnum.Exempt;

        if (src.IsOverdue)
            return DestinationEnum.Overdue;

        switch (src.Status)
        {
            case SourceEnum.InProgress:
                return DestinationEnum.InProgress;
            case SourceEnum.Complete:
                return DestinationEnum.Completed;
            case SourceEnum.AssessmentComplete:
                return DestinationEnum.ConfirmationPending;
            case SourceEnum.Submitted:
                return DestinationEnum.Submitted;
            case SourceEnum.NotSet:
                return DestinationEnum.Unset;
        }

        return DestinationEnum.None;
    }   
}
MapperConfiguration _config = new MapperConfiguration(config => 
{
    config.CreateMap<EntityClass, DestinationEnum>()
        .ConvertUsing(src => Helpers.ConvertFromEntityClassToDestinationEnum(src));
});

Demo Solution 1 & 2 @ .NET Fiddle

  • Related