AutoMapper不会更新嵌套集合。
创始人
2024-09-23 11:31:13
0

在使用AutoMapper时,确实无法直接更新嵌套集合。但可以通过编写自定义解析器来实现更新嵌套集合的功能。以下是一个示例:

假设有两个类:Source和Destination,它们具有相同的嵌套集合属性。

public class Source
{
    public int Id { get; set; }
    public List Children { get; set; }
}

public class ChildSource
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Destination
{
    public int Id { get; set; }
    public List Children { get; set; }
}

public class ChildDestination
{
    public int Id { get; set; }
    public string Name { get; set; }
}

为了更新嵌套集合,可以编写一个自定义解析器来处理映射逻辑。以下是一个示例:

public class CustomResolver : IValueResolver>
{
    private readonly IMapper _mapper;

    public CustomResolver(IMapper mapper)
    {
        _mapper = mapper;
    }

    public List Resolve(Source source, Destination destination, List destMember, ResolutionContext context)
    {
        if (destMember == null)
        {
            destMember = new List();
        }

        foreach (var childSource in source.Children)
        {
            var existingChild = destMember.FirstOrDefault(d => d.Id == childSource.Id);

            if (existingChild != null)
            {
                // 更新现有的子对象
                _mapper.Map(childSource, existingChild);
            }
            else
            {
                // 添加新的子对象
                var newChild = _mapper.Map(childSource);
                destMember.Add(newChild);
            }
        }

        // 删除已经不存在的子对象
        var idsToRemove = destMember.Select(d => d.Id).Except(source.Children.Select(c => c.Id)).ToList();
        destMember.RemoveAll(d => idsToRemove.Contains(d.Id));

        return destMember;
    }
}

然后,在配置AutoMapper映射时,使用该自定义解析器来处理嵌套集合属性。

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap()
        .ForMember(dest => dest.Children, opt => opt.MapFrom());
});

var mapper = config.CreateMapper();

var source = new Source
{
    Id = 1,
    Children = new List
    {
        new ChildSource { Id = 1, Name = "Child 1" },
        new ChildSource { Id = 2, Name = "Child 2" }
    }
};

var destination = new Destination
{
    Id = 1,
    Children = new List
    {
        new ChildDestination { Id = 1, Name = "Child 1" },
        new ChildDestination { Id = 3, Name = "Child 3" }
    }
};

mapper.Map(source, destination);

// 输出更新后的Destination对象
Console.WriteLine($"Destination Id: {destination.Id}");
foreach (var child in destination.Children)
{
    Console.WriteLine($"Child Id: {child.Id}, Name: {child.Name}");
}

在上述示例中,通过自定义解析器CustomResolver,我们可以实现更新嵌套集合的功能。解析器会根据源对象的集合属性来更新目标对象的集合属性,包括添加新的子对象、更新现有的子对象以及删除已经不存在的子对象。

请注意,以上示例仅供参考,实际使用时可能需要根据具体需求进行修改。

相关内容

热门资讯

教程攻略"福建十三水... 您好:这款福建十三水辅助游戏是可以开挂的,确实是有挂的,很多玩家在这款福建十三水辅助游戏中打牌都会发...
新手必备"天蝎辅助&... 新手必备"天蝎辅助"开挂(透视)wepoker轻量版辅助(有挂技术)wepoker轻量版辅助ai黑科...
玩家必看教程"斗棋联... 玩家必看教程"斗棋联盟俱乐部辅助挂"开挂(透视)hhpoker怎么防作弊(有挂攻略)1、下载安装好斗...
透视计算"开心酷跑破... 开心酷跑破解版 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、...
总算了解"白银胡乐亲... 白银胡乐亲友房开挂教程视频分享装挂详细步骤在当今的网络游戏中,白银胡乐亲友房作为一种经典的娱乐方式,...
记者爆料"玄龙辅助工... 您好:wepoker透视底牌脚本这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...
实操分享"葫芦娃七子... 实操分享"葫芦娃七子降妖破解版内购"开挂(透视)德扑圈透视(讲解有挂)1、下载安装好葫芦娃七子降妖破...
一分钟教会你"宝宝浙... 一分钟教会你"宝宝浙江游戏有没有辅助器"开挂(透视)wpk俱乐部辅助器(有挂解惑);无需打开直接搜索...
交流学习经验"多乐够... 交流学习经验"多乐够级辅助软件"开挂(透视)we poker游戏下(有挂技巧)《详细加薇136704...
大神推荐"打两圈兴化... 大神推荐"打两圈兴化辅助"开挂(透视)we-poker靠谱吗(有挂头条)1、下载安装好打两圈兴化辅助...