我正在从域模型映射到databaseDto。两者都有一个代表状态范围的枚举。
一旦通过映射器的配置(或在单元测试中)构建了映射器,是否可以通过自动映射器来检查一个枚举中所有值的映射方式
答案 0 :(得分:0)
您可以在下面使用示例代码;
public class Program
{
public static void Main(string[] args)
{
CreateMaps();
Lender src = new Lender()
{
Id = 1,
Name = "Bob",
ClaimTypes = ClaimType.A | ClaimType.C
};
LenderServiceModel dest = Mapper.Map<LenderServiceModel>(src);
Console.WriteLine("{0}: {1}", dest.Id, dest.Name);
foreach(var claimType in dest.ClaimTypes)
{
Console.WriteLine(claimType);
}
}
private static void CreateMaps()
{
Mapper.CreateMap<Lender, LenderServiceModel>()
.ForMember(dest => dest.ClaimTypes, opts => opts.MapFrom(src =>
src.ClaimTypes.ToString().Split(new string[]{", "}, StringSplitOptions.None).ToList()));
}
}
[Flags]
public enum ClaimType : int
{
A = 1,
B = 2,
C = 4
}
public class Lender
{
public int Id { get; set; }
public string Name { get; set; }
public ClaimType ClaimTypes { get; set; }
}
public class LenderServiceModel
{
public int Id { get; set; }
public string Name { get; set; }
public List<string> ClaimTypes { get; set; }
}