源和目标对象之间的映射,它具有作为具有不同值的枚举的公共属性。 [英] Mapping between source and destination object which has a common property as enum with different values.

查看:106
本文介绍了源和目标对象之间的映射,它具有作为具有不同值的枚举的公共属性。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有枚举属性的源对象和目标对象。对于每个对象,此枚举属性的成员之一是不同的。但我需要使用Automapper进行映射。我怎么能实现它?

示例代码片段:

源类:

公共类SourceClass

{

public SourceClassification SourceLevel

{

get;

set;

} < br $>
}



目的地类:

公共类DestinationClass

{

公共DestinationClassification输入

{

get;

set;

}

}



源类的枚举属性:

public enum SourceClassification

{

必需= 1,

可选= 2,

选择性= 3

}



目的地类的Enum财产:

公共枚举DestinationClassification

{

Mandate = 1,

可选= 2,

选择性= 3

}



我尝试了什么:



cfg.CreateMap< SourceClass,De stinationClass>()

.ForMember(s => s.Input,d => d.Input);

I have a source object and destination object with a enum property. One of the member of this enum property is different for each object. But I need to map it using Automapper. How I can achieve it?.
example code snippet:
Source Class:
public class SourceClass
{
public SourceClassification SourceLevel
{
get;
set;
}
}

Destination Class:
public class DestinationClass
{
public DestinationClassification input
{
get;
set;
}
}

Enum Property of Source Class:
public enum SourceClassification
{
Required = 1,
Optional = 2,
Selective = 3
}

Enum Property of Destination Class:
public enum DestinationClassification
{
Mandate= 1,
Optional = 2,
Selective = 3
}

What I have tried:

cfg.CreateMap<SourceClass,DestinationClass>()
.ForMember(s=>s.Input, d=>d.Input);

推荐答案

Just为枚举配置地图:



Just configure a map for the enums:

Mapper.CreateMap<SourceClassification,DestinationClassification>().ConvertUsing(val =>
{
   switch(val)
   {
      case SourceClassification.Required:
         return DestinationClassification.Mandate;
      case SourceClassification.Optional:
         return DestinationClassification.Optional;
      case SourceClassification.Selective:
         return DestinationClassification.Selective;
      default:
         return <whatever the default should be>;
   }
});





这有一个灵活的解决方案,所以如果enum是重构后你需要确保解决转换函数的变化。



This has the benefit of being a flexible solution, so if either enum is refactored you will need to make sure to address the change in the conversion function.


你只需要告诉 AutoMapper map SourceClass.SourceLevel to DestinationClass.Input 。它将映射enum成员的值,因此 Mandate 将映射到必需

You just need to tell AutoMapper to map SourceClass.SourceLevel to DestinationClass.Input. It will map on the value of the enum member so Mandate will map to Required.

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<SourceClass, DestinationClass>().ForMember(dest => dest.Input, opts =>
      opts.MapFrom(src => src.SourceLevel));
});

IMapper mapper = config.CreateMapper();
var source = new SourceClass
{
    SourceLevel = SourceClassification.Required,
};
var destination = mapper.Map<SourceClass, DestinationClass>(source);


这篇关于源和目标对象之间的映射,它具有作为具有不同值的枚举的公共属性。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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