且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

传递对象到AutoMapper映射

更新时间:2022-10-18 13:01:10

AutoMapper处理这个键值对的场景开箱。

  Mapper.CreateMap<信源,目的地>()
    .ForMember(D => d.Foo,选择=> opt.ResolveUsing(RES => res.Context.Options.Items [富));

然后在运行时:

  Mapper.Map<信源,目的地>(SRC,选择=> opt.Items [富] =酒吧);

详细有点挖成的背景项目,但你去那里。

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;

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.