我怎么AutoMapper不缓存映射的对象? [英] How do I get AutoMapper to not cache mapped objects?

查看:184
本文介绍了我怎么AutoMapper不缓存映射的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AutoMapper遇到一个已经被映射的对象,似乎再次使用该对象,而不是试图重新映射它。我相信它确实此基础上 .Equals()



我有一个的映射树。所以,有一些属性和子节点的节点。节点的多个具有 .Equals的相同的值(),因为它是基于关闭id属性。节点的孩子是不同的,我需要那些重新映射,但它使用一个缓存地图值。



有没有办法把缓存映射了吗?所有我能想到的是实现一个新的转换器,但是这完全违背了使用AutoMapper的目的。



下面是如何重现一个例子。

 无效的主要()
{
无功源=新的List<标记和GT;
{
新标签
{
ID = 1,
NAME =标签1,
ChildTags =新的List<标签>
{
新标签
{
ID = 2,
NAME =标签2,
ChildTags =新的List<标签>
{
新代码{ID = 3,名称=标签3},
新代码{ID = 4,名称=标签4}
}
}
}
},
新代码{ID = 1,名称=标记1},
新标签
{
ID = 3 ,NAME =标签3,ChildTags =新的List<标记和GT;
{
新代码{ID = 4,名称=标签4}
}
}
};

Mapper.CreateMap<标签,标签>();
VAR的结果= Mapper.Map<&IList的LT;标签>中的IList<标记和GT;>(源);

results.Dump();
}

公共类标签
{
公众诠释标识{搞定;组; }
公共字符串名称{;组; }
公开的IEnumerable<标记和GT; ChildTags {搞定;组; }

公众覆盖布尔等于(obj对象)
{
如果(OBJ == NULL)
{
返回FALSE;
}

变种X =这一点;
变种Y =(TAG)OBJ;

返回x.Id.Equals(y.Id);
}

公共覆盖INT的GetHashCode()
{
返回Id.GetHashCode();
}
}


解决方案

有。现在禁用缓存选项

  Mapper.CreateMap<标签,标签>(); 
VAR的结果= Mapper.Map<&IList的LT;标签>中的IList<标记和GT;>(源,选择=> opt.DisableCache = TRUE);


When AutoMapper encounters an object that's already been mapped, it seems to use that object again, instead of trying to re-map it. I believe it does this based on .Equals().

I have a tree that's being mapped. So, a node with some properties, and children. More than one of the nodes have the same value of .Equals(), because it's based off an Id property. The children of the nodes are different and I need those re-mapped, but it's using a cached map value.

Is there a way to turn the cached mapping off? All I can think of is implementing a new converter, but that totally defeats the purpose of using AutoMapper.

Here is an example on how to reproduce.

void Main()
{
    var source = new List<Tag>
    {
        new Tag 
        { 
            Id = 1, 
            Name = "Tag 1", 
            ChildTags = new List<Tag>
            {
                new Tag 
                { 
                    Id = 2, 
                    Name = "Tag 2", 
                    ChildTags = new List<Tag> 
                    {
                        new Tag {Id = 3, Name = "Tag 3"},
                        new Tag {Id = 4, Name = "Tag 4"}
                    }
                }
            }
        },
        new Tag { Id = 1, Name = "Tag 1" },
        new Tag 
        {
            Id = 3, Name = "Tag 3", ChildTags = new List<Tag>
            {
                new Tag {Id = 4, Name = "Tag 4"}
            }
        }
    };

    Mapper.CreateMap<Tag, Tag>();
    var results = Mapper.Map<IList<Tag>, IList<Tag>>(source);

    results.Dump();
}

public class Tag
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IEnumerable<Tag> ChildTags { get; set; }

    public override bool Equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }

        var x = this;
        var y = (Tag)obj;

        return x.Id.Equals(y.Id);
    }

    public override int GetHashCode()
    {
        return Id.GetHashCode();
    }
}

解决方案

There is now an option to disable the cache.

Mapper.CreateMap<Tag, Tag>();
var results = Mapper.Map<IList<Tag>, IList<Tag>>(source, opt => opt.DisableCache = true);

这篇关于我怎么AutoMapper不缓存映射的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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