简体   繁体   中英

AutoMapper one source object to multiple destination objects

I've got a source model defined as

public class SourceRoot
{
    ...
    public Organisation Organisation { get; set; }
    ...
}

public class Organisation
{
    public long? Id { get; set; }
    public string Name { get; set; }    
    public string Currency { get; set; }   
    public double SupplementaryAmount { get; set; }
    public decimal BaseConversionRate { get; set; }
}

and a destination defined as:

public class DestinationRoot
{
    ...
    public Organisation Organisation { get; set; }
    public ContributesTo ContributesTo { get; set; }
}

public class Organisation
{
    public long? Id { get; set; }
    public string Name { get; set; }        
}

public class ContributesTo
{
    public string Currency { get; set; }
    public double SupplementaryAmount { get; set; }
    public decimal BaseConversionRate { get; set; }
}

I want to map from the SourceRoot to the DestinationRoot add copy from the source Organisation to the destination Organisation AND ContributesTo .

I have the following configuration for AutoMapper:

public static class AutoMapperConfig
{
    public static MapperConfiguration RegisterMappings()
    {
        var config = new MapperConfiguration(cfg => 
            {   
                cfg.AddProfile<MyProfile>();
            });

        return config;
    }
}

public class MyProfile : Profile
{
    protected override void Configure()
    {
        this.CreateMap<SourceRoot, DestinationRoot>();        
        this.CreateMap<Source.Organisation, Destination.Organisation>();
        this.CreateMap<Source.Organisation, Destination.ContributesTo>();
    }
}

Using this current profile the Organisation gets mapped but the ContributesTo comes out as null. Note that I'm using version 4.2 of AutoMapper where the static methods have been deprecated so trying to steer away from that. Normally I would do:

Mapper.CreateMap<SourceRoot, DestinationRoot>()
            .ForMember(d => d.ContributesTo, opt => opt.MapFrom( s=> Mapper.Map<ContributesTo>(s.Organisation)));

But this is not advised anymore (referencing the static methods). Is there an alternative way of doing this?

Thanks

Just add mapping for ContributesTo destination member:

protected override void Configure ()
{
    CreateMap<Source.SourceRoot, Destination.DestinationRoot>()
        .ForMember(d => d.ContributesTo, opt => opt.MapFrom(s => s.Organisation));

    CreateMap<Source.Organisation, Destination.Organisation>();
    CreateMap<Source.Organisation, Destination.ContributesTo>();
}

Otherwise Automapper finds that both source and destinaton roots have property Organisation and it maps only this property. Automapper cannot understand that it should use one property of source to map several properties of destination (which do not match by name). Note that you don't need to specify mapping for Organisation member, because it matches property name in destination object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM