Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Objects to AutoMapper Mapping

I am working with AutoMapper and some of the values for the entity being mapped to are variables in my current method. I have tried to Google it but to no avail. Can I pass a set of KeyValue Pairs or an object or something to my mapping to have it use those values?

Sample of Post Mapping Modification

//comment variable is a Comment class instance
var imageComment = AutoMapper.Mapper.Map<Data.ImageComment>(comment);
//I want to pass in imageId so I dont have to manually add it after the mapping
imageComment.ImageId = imageId;
like image 209
CWitty Avatar asked Jul 31 '15 14:07

CWitty


People also ask

Why you should not use AutoMapper?

1. If you use the convention-based mapping and a property is later renamed that becomes a runtime error and a common source of annoying bugs. 2. If you don't use convention-based mapping (ie you explicitly map each property) then you are just using automapper to do your projection, which is unnecessary complexity.

How do I use ForMember in AutoMapper?

CreateMap<EFAddress, Address>() . ForMember(dest => dest. Code, opt => opt. MapFrom(src => src.Name));

Is AutoMapper faster than manual mapping?

Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping. This test was done on 100,000 records and I must say I was shocked.

How do I use AutoMapper to list a map?

How do I use AutoMapper? First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members.


3 Answers

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.

like image 141
Jimmy Bogard Avatar answered Sep 26 '22 00:09

Jimmy Bogard


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";
    }
);
like image 36
MUG4N Avatar answered Sep 25 '22 00:09

MUG4N


For Automapper 6.0.2:

Profile:

public class CoreProfile : Profile
{
    public CoreProfile()
    {
        CreateMap<Source, Dest>()
            .ForMember(d => d.Foo,
                opt => opt.ResolveUsing(
                    (src, dst, arg3, context) => context.Options.Items["Foo"]
                )
            );
    }
}

Mapping:

    var result = Mapper.Map<PlanResult>(aa, opt => {
        opt.Items["Foo"] = 2;
        opt.Items["OtherFoo"] = 1000;
    });
like image 45
Leszek P Avatar answered Sep 25 '22 00:09

Leszek P