Reputation: 1906
I wondering if it is possible to “sub map” mapped objects in ForMemeber. I have a mapping for class a to b configured. Then I have two new classes I need to map c and d, but the structure is different so I have to use ForMember to map configure mapping between them. But both c and d has properties with classes that users a in source and b in destination. Is there a way to say use the already configured mapping for the inner a to b mapping for the properties in c and d?
Upvotes: 0
Views: 1834
Reputation: 5144
If I understand this right, you have something like
public class a
{
public int Foo { get; set; }
}
public class b
{
public int Bar { get; set; }
}
public class c
{
public a Baz { get; set; }
}
public class d
{
public b Qux { get; set; }
}
And you want to the properties in classes c
and d
to be mapped without having to redefine the mapping for a
and b
?
If so, you get that by default. Something like this should do it:
public static class AutoMapperConfigurator
{
public static void Configure()
{
AutoMapper.Mapper.CreateMap<a, b>()
.ForMember(dest => dest.Bar, opt => opt.MapFrom(src => src.Foo));
AutoMapper.Mapper.CreateMap<c, d>()
.ForMember(dest => dest.Qux, opt => opt.MapFrom(src => src.Baz));
AutoMapper.Mapper.AssertConfigurationIsValid();
}
}
If you instead meant something else, please update your question to clarify.
Upvotes: 1