如何使用自动映射器映射使用不同大小写的2个枚举 [英] How can I use automapper to map 2 enums that use different casing

查看:48
本文介绍了如何使用自动映射器映射使用不同大小写的2个枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多枚举要保留所有必须映射到另一个完全没有标准的系统(大写,无大写,帕斯卡,骆驼)的枚举.我找不到自动映射器标志来告诉它忽略枚举的大小写.我可以为每个枚举使用一个custome转换器,但由于数量太多,我希望使用通用转换器.

I have many enums that I want to keep all caps that I have to map to another system that has no standard at all (caps, no caps, pascal, camel). I can't find an automapper flag to tell it to ignore case for enums. I could use a custome converter for each enum but I would prefer a generic converter since there are so many.

这里的一些答案暗示自动映射器已经做到了这一点.我没有从测试中得到.

Some answers here have implied that automapper does this already. I don't get that from my testing.

如果我有这些枚举:

public enum AllCaps
{
    VALUE1,
    VALUE2,
    VALUE3
}

public enum NoCaps
{
    value1,
    value2,
    value3
}

public enum MixedCaps
{
    Value1,
    Value2,
    Value3
}

这些地图:

CreateMap<AllCaps, NoCaps>();
CreateMap<AllCaps, MixedCaps>();
CreateMap<NoCaps, AllCaps>();
CreateMap<NoCaps, MixedCaps>();
CreateMap<MixedCaps, AllCaps>();
CreateMap<MixedCaps, NoCaps>();

此代码:

var vAllCaps = new AllCaps();
var vNoCaps = new NoCaps();
var vMixedCaps = new MixedCaps();

vAllCaps = AllCaps.VALUE2;
vNoCaps = NoCaps.value2;
vMixedCaps = MixedCaps.Value2;

var AllCapsToNoCaps = Mapper.Map<AllCaps, NoCaps>(vAllCaps);
var AllCapsToMixedCaps = Mapper.Map<AllCaps, MixedCaps>(vAllCaps);
var NoCapsToAllCaps = Mapper.Map<NoCaps, AllCaps>(vNoCaps);
var NoCapsToMixedCaps = Mapper.Map<NoCaps, MixedCaps>(vNoCaps);
var MixedCapsToAllCaps = Mapper.Map<MixedCaps, AllCaps>(vMixedCaps);
var MixedCapsToNoCaps = Mapper.Map<MixedCaps, NoCaps>(vMixedCaps);

我所有映射变量的结果均为VALUE1,value1或Value1,而不是预期的VALUE2,value2或Value2.

The result of all my mapped variables are either VALUE1, value1, or Value1 and not the expected VALUE2, value2, or Value2.

推荐答案

Automapper自动在枚举之间进行不区分大小写的映射.不需要对枚举的 CreateMap< EnumSource,EnumDest>()调用.

Automapper does a case insensitive mapping between enumerations automatically. No CreateMap<EnumSource, EnumDest>() call for an enum is required.

有一些细节值得一提:

  • 映射类型中嵌入的枚举值是不区分大小写的映射
  • configuration.AssertConfigurationIsValid()不检查目标值是否可用于所有源值
  • 如果目标枚举中不存在源值,则采用数值
  • Enum values embedded inside a mapped type are case insensitive mapped
  • configuration.AssertConfigurationIsValid() Does not check if a target value is available for all source values
  • If a source value does not exist in the target enumeration, the numeric value is taken

AutoMapper.Extensions.EnumMapping 可以验证枚举映射,但是此扩展名进行区分大小写的检查.对于一个简单的枚举到枚举的映射,实际上并不是必需的.

The AutoMapper.Extensions.EnumMapping can validate the enumeration mappings, but this extension does a case sensitive check. And it is not really required for a simple enum to enum mapping.

可以添加自定义验证来检查有效的枚举映射

A custom validation can be added to check valid enum mappings

示例代码:

config.Advanced.Validator(context =>
            {
                if (context.TypeMap != null)
                {
                    foreach (var memberMap in context.TypeMap.MemberMaps)
                    {
                        if (memberMap.SourceType.IsEnum && memberMap.DestinationType.IsEnum)
                        {
                            var hasMappingError = false;
                            var sourceEnumValue = Enum.GetValues(memberMap.SourceType).Cast<object>()
                                .Select(x => x.ToString().ToLowerInvariant()).ToList();
                            var targetEnumValues = Enum.GetValues(memberMap.DestinationType).Cast<object>()
                                .Select(x => x.ToString().ToLowerInvariant()).ToList();

                            var messageBuilder =
                                new StringBuilder(
                                    $"Missing enum mapping from {memberMap.SourceType.FullName} to {memberMap.DestinationType.FullName}");

                            messageBuilder.AppendLine();
                            messageBuilder.AppendLine("The following source values are not mapped:");

                            foreach (var sourceValue in sourceEnumValue)
                            {
                                if (!targetEnumValues.Contains(sourceValue))
                                {
                                    hasMappingError = true;
                                    messageBuilder.AppendLine($" - {sourceValue}");
                                }
                            }

                            if (hasMappingError)
                            {
                                throw new AutoMapperConfigurationException(messageBuilder.ToString());
                            }
                        }
                    }
                }
            });

这篇关于如何使用自动映射器映射使用不同大小写的2个枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆