在AutoMapper地图中的foreach中额外迭代 [英] Extra iterations in a foreach in an AutoMapper map

查看:213
本文介绍了在AutoMapper地图中的foreach中额外迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



地图定义:

  Mapper.CreateMap< AdminGameEditModel,Game>()
.BeforeMap((s,d)=>
{$ b $ (foreground(var platId in s.PlatformIDs)
{
Platform newPlat = _gameRepository.GetPlatform(platId);

d.Platforms.Add(newPlat);
}
.ForMember(dest => dest.BoxArtPath,opt => opt.Ignore())
.ForMember(dest => dest.IndexImagePath,opt => ; opt.Ignore())
.ForMember(dest => dest.Cons,opt => opt.MapFrom(src => String.Join(|,src.Cons)))
.ForMember(dest => dest.Pros,opt => opt.MapFrom(src => String.Join(|,src.Pros)))
.ForMember(dest => dest.LastModified,opt => opt.UseValue(DateTime.Now))
.ForMember (dest => dest.Platforms,opt => opt.Ignore());

BeforeMap 原因,多次遍历s.PlatformIDs数组。所以,例如,如果它包含两个值,我会得到六个或更多的迭代,重复两个期望的值。 PlatformIDs 不是定义为二维数组,调试器确认数组只包含它应该的值,没有重复集。



我很难过,可能是什么原因造成的。






编辑:使用循环,我有以下断点 - $ / $>

  .BeforeMap((s,d)=> 
{
foreach(var platId在s.PlatformIDs)//断点1
{
Platform newPlat = _gameRepository.GetPlatform(platId); // breakpoint 2

d.Platforms.Add(newPlat);
} //断点3
})

第一遍是正常的 - 断点1 - >断点2 - >断点3.然后它将回到2,然后回到3,这是预期的。有什么奇怪的是,它会跳回到断点1,并再次启动过程。

我不确定是否有模式。两个数组值导致六遍。编辑2:我的预感是正确的 - BeforeMap $ <$> / code>不止一次发射。




编辑3:问题仍然存在于 AfterMap 以及。每个映射执行多次。

解决方案

似乎是一个合法的bug,通过类似的判断: http://automapper.codeplex.com/workitem/6604 。我在AutoMapper的GitHub上把它写成了一个问题,并且把这个问题与这个问题联系起来,这样开发者就可以看到我正在尝试做什么。


For some reason, a loop that I use in an AutoMapper map definition is iterating more than it should.

The map definition:

    Mapper.CreateMap<AdminGameEditModel, Game>()
        .BeforeMap((s, d) =>
        {
            foreach (var platId in s.PlatformIDs)
            {
                Platform newPlat = _gameRepository.GetPlatform(platId);

                d.Platforms.Add(newPlat);
            }
        })
        .ForMember(dest => dest.BoxArtPath, opt => opt.Ignore())
        .ForMember(dest => dest.IndexImagePath, opt => opt.Ignore())
        .ForMember(dest => dest.Cons, opt => opt.MapFrom(src => String.Join("|", src.Cons)))
        .ForMember(dest => dest.Pros, opt => opt.MapFrom(src => String.Join("|", src.Pros)))
        .ForMember(dest => dest.LastModified, opt => opt.UseValue(DateTime.Now))
        .ForMember(dest => dest.Platforms, opt => opt.Ignore());

The foreach in BeforeMap will, for some reason, iterate over the s.PlatformIDs array multiple times. So, for example, if it contains two values, I'll get six or more iterations, with the two expected values repeating. The PlatformIDs are not defined as a two dimensional array, and the debugger confirms that the array only contains the values it should, with no repeating sets.

I'm stumped as to what could be causing it.


EDIT: With the loop, I have the following breakpoints -

.BeforeMap((s, d) =>
{
    foreach (var platId in s.PlatformIDs) // breakpoint 1
    {
        Platform newPlat = _gameRepository.GetPlatform(platId); // breakpoint 2

        d.Platforms.Add(newPlat);
    } // breakpoint 3
})

The first pass is normal - breakpoint 1 -> breakpoint 2 -> breakpoint 3. It will then go back to 2, then to 3, which is expected. What's weird is that it will then jump back to breakpoint 1, and start the process over again.

I'm not sure if there's a pattern. Two array values results in six passes. One array value results in four passes.


EDIT 2: My hunch was right - BeforeMap is firing more than once.


EDIT 3: The problem persists in AfterMap as well. The method executes more than once per mapping.

解决方案

Appears to be a legit bug, judging by something similar: http://automapper.codeplex.com/workitem/6604. I've written it up as an issue on AutoMapper's GitHub, and have linked that issue to this question so the devs can see what I was attempting to do.

这篇关于在AutoMapper地图中的foreach中额外迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆