Frederic
Frederic

Reputation: 2065

How not to explicitly specify members for which the name automatically match up?

I am using AutoMapper 9.0 and in the example below, I am mapping a Person to a People Object.

Because 1 member differs from Person to People (Person has Sfx while People has Suffix), I have to specifically map the rest of the properties that would otherwise automatically match up.

Is there a way to not specify them but for them to still be mapped ?

    configurationExpression.CreateMap<JsonRequest, XmlRequest>()
                .ForMember(
                    dest => dest.People,
                    opt => opt.MapFrom(src => new People
                    {
                        FirstName = src.Person.FirstName,
                        MiddleName = src.Person.MiddleName,
                        LastName = src.Person.LastName,
                        Suffix = src.Person.Sfx
                    }));

Upvotes: 0

Views: 84

Answers (1)

Hooman Bahreini
Hooman Bahreini

Reputation: 15579

Checking the documentation, you should be able to achieve this by defining separate mappings for your OutterClass and InnerClass:

var config = new MapperConfiguration(cfg => {
    cfg.CreateMap<OuterSource, OuterDest>();
    cfg.CreateMap<InnerSource, InnerDest>();
});

Have you tried something like this?

configurationExpression.CreateMap<Person, People>()
    .ForMember(dest => dest.Suffix, opt => opt.MapFrom(src => src.sfx))
    .ReverseMap(); 

configurationExpression.CreateMap<JsonRequest, XmlRequest>()
    .ForMember(dest => dest.People, opt => opt.MapFrom(src => src.Person))
    .ReverseMap();

Upvotes: 2

Related Questions