Pass Objects to AutoMapper Mapping
AutoMapper 9.0.0
As of version 8.0.0 the API of AutoMapper has been changed. In doing so ResolveUsing
was consolidated with MapFrom
. Take a look at the corresponding pull request for further information.
Profile
public class CoreProfile : Profile
{
public CoreProfile()
{
CreateMap<Source, Destination>()
.ForMember(d => d.Bar,
opt => opt.MapFrom(
(src, dst, _, context) => context.Options.Items["bar"]
)
);
}
}
Mapping
var destination = mapper.Map<Destination>(
source,opt => {
opt.Items["bar"] = "baz";
}
);
AutoMapper handles this key-value pair scenario out of the box.
Mapper.CreateMap<Source, Dest>()
.ForMember(d => d.Foo, opt => opt.ResolveUsing(res => res.Context.Options.Items["Foo"]));
Then at runtime:
Mapper.Map<Source, Dest>(src, opt => opt.Items["Foo"] = "Bar");
A bit verbose to dig into the context items but there you go.