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,我们可以实现更新嵌套集合的功能。解析器会根据源对象的集合属性来更新目标对象的集合属性,包括添加新的子对象、更新现有的子对象以及删除已经不存在的子对象。

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

相关内容

热门资讯

教程辅助!智星德州菠萝有没有挂... 教程辅助!智星德州菠萝有没有挂,德州ai辅助有用,爆料教程(原来是有挂)1、金币登录送、破产送、升级...
透视工具!hhpoker透视脚... 透视工具!hhpoker透视脚本下载,福建天天开心辅助器,透明教程(有挂透视);福建天天开心辅助器软...
记者揭秘!aa扑克有挂吗,we... 一、wepower有外挂简介了解软件请加微:136704302wepower有外挂是一款在线扑克游戏...
透视最新!aapoker可以开... 透视最新!aapoker可以开挂吗,途游四川小程序脚本辅助,软件教程(有挂神器)1、不需要AI权限,...
我来分享!wpk德州胜利跟号有... 我来分享!wpk德州胜利跟号有关么,aapoker辅助工具存在,介绍教程(本来有挂)您好,aapok...
透视脚本!wepoker辅助器... 透视脚本!wepoker辅助器怎么用,顺欣茶楼怎么开挂,2025版教程(有挂透明)是一款可以让一直输...
揭秘真相!wpk ai机器人和... 揭秘真相!wpk ai机器人和真的的区别,wepoke有没有挂,AI教程(一贯真的有挂);致您一封信...
透视脚本!hhpoker透视功... 透视脚本!hhpoker透视功能如何下载,wepoker辅助软件价格,大神讲解(有挂方法);致您一封...
交流学习经验!aapoker线... 一、WPK透视辅助简介了解软件请加微:136704302WPK透视辅助是一款在线扑克游戏平台,玩家可...
透视透视!wepoker辅助工... 透视透视!wepoker辅助工具,多乐辅助下载够机,攻略方法(有挂技巧)1、很好的工具软件,可以解锁...