自动映射合并对象问题

时间:2016-04-18 18:35:25

标签: c# .net automapper

让自动播放器工作后(previous question),我正在努力解决另一个问题(把它带到另一个问题,所以第一个问题不会太复杂)...

我有下一堂课:

public class Model1
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDay { get; set; }
    public int Gender { get; set; }
    public string NickName { get; set; }
}    
public class Model2
{
    public bool Married { get; set; }    
    public int Children { get; set; }
    public bool HasPet { get; set; }
}

public class Entity1
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime BirthDay { get; set; }
    public int Gender { get; set; }
}    
public class Entity2
{
    public bool Married { get; set; }    
    public int Children { get; set; }
    public bool HasPet { get; set; }
    public string NickName { get; set; }
}

除了名称和复杂性之外,这些对象与我的原始对象示意性地类似。

和AutoMapper配置类(从Global.asax调用):

public class AutoMapperConfig
{
    public static MapperConfiguration MapperConfiguration { get; set; }

    public static void Configure()
    {
        MapperConfiguration = new MapperConfiguration(cfg => {
            cfg.AddProfile<Out>();
            cfg.CreateMap<SuperModel, SuperEntity>();
        });
        MapperConfiguration.AssertConfigurationIsValid();
    }
}

public class Out: Profile
{
   protected override void Configure()
    {
        CreateMap<Model1, Entity1>();
        CreateMap<Model2, Entity2>()
            .ForMember(dest => dest.NickName, opt => opt.Ignore());
        CreateMap<Model1, Entity2>()
            .ForMember(dest => dest.Married, opt => opt.Ignore())
            .ForMember(dest => dest.Children, opt => opt.Ignore())
            .ForMember(dest => dest.HasPet, opt => opt.Ignore());
        CreateMap<SuperModel, SuperEntity>()
            .ForMember(dest => dest.Entity1, opt => opt.MapFrom(src => src.Model1))
            .ForMember(dest => dest.Entity2, opt => opt.MapFrom(src => src.Model2));
    }
}

当我需要转换对象时,我会做下一步(此时我已_superModel初始化并填充数据):

SuperEntity _superEntity = new SuperEntity();
AutoMapperConfig.MapperConfiguration.CreateMapper().Map<SuperModel, SuperEntity>(_superModel, _superEntity);

所以,我将Model1映射到Entity1(女巫很好),还Model2映射到Entity2(女巫也很好,除了Id属性,这是忽略)。

主要对象SuperModelSuperEntity也会被映射,似乎工作正常。

当我将Model1映射到Entity2时,问题就出现了,以获取NickName(认为其他属性被忽略)。一些如何总是null

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

问题是您要从Entity2的多个源属性值(ChildrenMarriedHasPet映射目标属性(Model2)并且来自Nickname的{​​{1}}。

您可以使用Custom ResolverAfterMap方法解决问题。

使用自定义解析器

您必须创建一个继承自Model1的类,以定义如何从ValueResolver映射Entity2

SuperModel

然后,像这样使用它:

public class CustomResolver : ValueResolver<SuperModel, Entity2>
{
    protected override Entity2 ResolveCore(SuperModel source)
    {
        return new Entity2
        {
            Children = source.Model2.Children,
            HasPet = source.Model2.HasPet,
            Married = source.Model2.Married,
            NickName = source.Model1.NickName
        };
    }
}

使用AfterMap

您可以在映射后执行操作,因此请在地图后面将CreateMap<SuperModel, SuperEntity>() .ForMember(dest => dest.Entity1, opt => opt.MapFrom(src => src.Model1)) .ForMember(dest => dest.Entity2, opt => opt.ResolveUsing<CustomResolver>()); 的值传递给Model1.Nickname

Entity2.Nickname