简体   繁体   English

使用 Automapper 映射两个集合时如何使用 BeforeMap Action?

[英]How to use BeforeMap Action when mapping two Collections with Automapper?

When mapping two single objects with automapper, the BeforeMap Action is working fine:使用 automapper 映射两个单个对象时, BeforeMap操作工作正常:

return _mapper.Map<Project, ProjectDto>(projects, opt =>
{
    opt.BeforeMap((src, dest) => dest.ProvidedLanguage = requestedLanguage);
});

But when mapping two collections, BeforeMap does not work as src and dest are of type object now:但是当映射两个集合时, BeforeMap不起作用,因为srcdest现在是object类型:

return _mapper.Map<IEnumerable<Project>, IEnumerable<ProjectDto>>(projects, opt =>
{
    opt.BeforeMap((src, dest) => dest.ProvidedLanguage = requestedLanguage);
});

I need the provided language for the mapping itself, so I cannot set it after mapping.我需要为映射本身提供语言,所以我无法在映射后设置它。 How can I pass the provided language to the destination objects when mapping two collections?映射两个集合时,如何将提供的语言传递给目标对象?

I premliminary solved it like this:我初步是这样解决的:

        var result = new List<ProjectDto>();
        foreach (var item in projects)
        {
            var dest = new ProjectDto() { ProvidedLanguage = requestedLanguage };
            result.Add(_mapper.Map(item, dest));
        }

I know, that it doesn't look elegant.我知道,它看起来并不优雅。 if anybody knows a better way to solve it, don't hesitate to share your idea.如果有人知道更好的解决方法,请不要犹豫,分享您的想法。

Using BeforeMap() to tweak destination object is dangerous.使用BeforeMap()来调整目标对象是危险的。 Running your code:运行你的代码:

return _mapper.Map<Project, ProjectDto>(projects, opt =>
{
    opt.BeforeMap((src, dest) => dest.ProvidedLanguage = requestedLanguage);
});

will result in a NullReferenceException as dest object was not initialized yet by AutoMapper.将导致NullReferenceException因为dest对象尚未被 AutoMapper 初始化。 See, BeforeMap() works best at performing some tweaks over source object, because any work over destination object can be overridden by forthcoming mapping process.看, BeforeMap()最适合对对象执行一些调整,因为对目标对象的任何工作都可以被即将到来的映射过程覆盖。 If you want to do tweaks to your destination object, it's better to use AfterMap() .如果您想对目标对象进行调整,最好使用AfterMap() That way you'll have the mapping process already over and the certainty that your destination object was initialized.这样,您的映射过程就已经结束,并且您的目标对象已被初始化。 I'm not saying you shouldn't use BeforeMap() , I'm just saying be cautious with it, and use it when you really know what you're doing.我并不是说你不应该使用BeforeMap() ,我只是说要谨慎使用它,并在你真正知道自己在做什么时使用它。

As of mapping the collections, they may seem to be object type, but that's just what they seem to be.至于映射集合,它们似乎是object类型,但这正是它们的样子。 Put a breakpoint in debugger to see their type is correct.在调试器中放置一个断点以查看它们的类型是否正确。

And you won't bee able to set ProvidedLanguage property on IEnumerable .而且您将无法在IEnumerable上设置ProvidedLanguage属性。 If you want to set that property on every ProjectDto that's inside that IEnumerable , use a foreach loop for that:如果要在IEnumerable内的每个ProjectDto上设置该属性,请为此使用foreach循环:

return _mapper.Map<IEnumerable<Project>, IEnumerable<ProjectDto>>(projects, opt =>
{
    opt.AfterMap((source, destination) =>
    {
        foreach (var dto in destination)
        {
            dto.ProvidedLanguage = requestedLanguage;
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM