AutoMapper字符串到枚举描述 [英] AutoMapper strings to enum descriptions

查看:255
本文介绍了AutoMapper字符串到枚举描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定要求:

获取对象图,根据第二个字符串属性的已处理值设置所有枚举类型属性。公约规定,源字符串属性的名称将是具有后缀Raw的枚举属性的名称。

Take an object graph, set all enum type properties based on the processed value of a second string property. Convention dictates that the name of the source string property will be that of the enum property with a postfix of "Raw".

通过处理,我们意味着我们需要删除指定的字符等

By processed we mean we'll need to strip specified characters e.t.c.

我看过自定义格式化程序,价值解析器和类型转换器,这些都不是一个解决方案?

I've looked at custom formatters, value resolvers and type converters, none of which seems like a solution for this?

我们要使用AutoMapper而不是我们自己的反射例程有两个原因:a)它在项目的其余部分广泛使用,b)它给出递归遍历ootb。

We want to use AutoMapper as opposed to our own reflection routine for two reasons, a) it's used extensively throughout the rest of the project and b) it gives you recursive traversal ootb.

- 示例 -

给定下面的(简单)结构,

Given the (simple) structure below, and this:

var tmp = new SimpleClass 
  { 
       CountryRaw = "United States",
       Person = new Person { GenderRaw="Male" }
  };

var tmp2 = new SimpleClass();

Mapper.Map(tmp, tmp2);

我们预计tmp2的MappedCountry枚举将为Country.UnitedStates,Person属性具有

we'd expect tmp2's MappedCountry enum to be Country.UnitedStates and the Person property to have a gender of Gender.Male.

public class SimpleClass1
{
  public string CountryRaw {get;set;}

  public Country MappedCountry {get;set;}

  public Person Person {get;set;}
}

public class Person
{
  public string GenderRaw {get;set;}

  public Gender Gender {get;set;}

  public string Surname {get;set;}
}

public enum Country
{
  UnitedStates = 1,
  NewZealand = 2
}

public enum Gender
{
  Male,
  Female,
  Unknown
}

谢谢

推荐答案

我使用 ValueInjecter
这是整个事情:

I did it with the ValueInjecter, here is the whole thing:

我已经添加了一个支柱到S impleClass只是为了向你展示如何工作

I've added one more prop to the SimpleClass just to show you how it works

public class SixFootUnderTest
{
    [Test]
    public void Test()
    {
        var o = new SimpleClass1
                    {
                        CountryRaw = "United States",
                        GenderRaw = "Female",
                        Person = new Person { GenderRaw = "Male" }
                    };

        var oo = new SimpleClass1();

        oo.InjectFrom(o)
            .InjectFrom<StrRawToEnum>(o);
        oo.Person.InjectFrom<StrRawToEnum>(o.Person);

        oo.Country.IsEqualTo(Country.UnitedStates);
        oo.Gender.IsEqualTo(Gender.Female);
        oo.Person.Gender.IsEqualTo(Gender.Male);
    }

    public class SimpleClass1
    {
        public string CountryRaw { get; set; }

        public Country Country { get; set; }

        public string GenderRaw { get; set; }

        public Gender Gender { get; set; }

        public Person Person { get; set; }
    }

    public class Person
    {
        public string GenderRaw { get; set; }

        public Gender Gender { get; set; }

        public string Surname { get; set; }
    }


    public class StrRawToEnum : LoopValueInjection
    {
        protected override bool UseSourceProp(string sourcePropName)
        {
            return sourcePropName.EndsWith("Raw");
        }

        protected override string TargetPropName(string sourcePropName)
        {
            return sourcePropName.RemoveSuffix("Raw");
        }

        protected override bool TypesMatch(Type sourceType, Type targetType)
        {
            return sourceType == typeof(string) && targetType.IsEnum;
        }

        protected override object SetValue(object sourcePropertyValue)
        {
            return Enum.Parse(TargetPropType, sourcePropertyValue.ToString().Replace(" ", ""), true);
        }
    }

    public enum Country
    {
        UnitedStates = 1,
        NewZealand = 2
    }


    public enum Gender
    {
        Male,
        Female,
        Unknown
    }
}

如果你需要从CountryRaw到MappedCountry
,你可以这样做:

also in case you need to do it from CountryRaw to MappedCountry you could do it like this:

oo.InjectFrom(new StrRawToEnum().TargetPrefix("Mapped"), o);

这篇关于AutoMapper字符串到枚举描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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