I convert my enums into EnumViewObject's and back again, which I do in the following way:
public class EnumViewObjectToEnumConverter : ITypeConverter<EnumViewObject, Enum>
{
public Enum Convert(ResolutionContext context)
{
var enumViewObject = (EnumViewObject) context.SourceValue;
return enumViewObject.ToEnum();
}
}
Mapper.CreateMap<EnumViewObject, Enum>()
.ConvertUsing<EnumViewObjectToEnumConverter>();
That works for all non-nullable enum properties.
The problem is that it does not work when I am mapping into a nullable enum properties defined like this:
public TSortableFields? SortBy { get; set; } = null;
public SortOrder? SortOrder { get; set; } = null;
I Tried using an open generic version, but that doesn't catch it either:
Mapper.CreateMap(typeof(EnumViewObject), typeof(Nullable<>))
How do I do this, if I want to avoid writing stuff like this for every property i map:
Mapper.CreateMap<FindViewObjRequest<PriceListViewObj>, FindPriceListsRequest>()
.ForMember(dest => dest.SortBy, opt => opt.MapFrom(src => src.SortBy.ToEnum<PriceListsSortableFields>()))
.ForMember(dest => dest.SortOrder, opt => opt.MapFrom(src => src.SortOrder.ToEnum<SortOrder>()));
For completeness I am getting errors like this : (For the SortOrder as example since that is just a normal nullable enum and not the generic version, which might matter)
Missing type map configuration or unsupported mapping.
Mapping types:
EnumViewObject -> Nullable`1
Ed.Bio.Client.AppLayer.Shared.DataBinding.EnumViewObject -> System.Nullable`1[[Ed.Bio.Shared.Domain.Enums.Common.SortOrder, Ed.Bio.Shared.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
Destination path:
FindPriceListsRequest.SortOrder.SortOrder
Source value:
#[85180436-4fdf-4c7a-b548-4e2daa3d5e14] - Descending - IsSelected:False
/Jesper Risager