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有没有插件,德普之星透视辅助,原来是有挂(有挂解密)进入游戏-大厅左侧-新...
于此同时!佛手在线大菠萝技巧,... 于此同时!佛手在线大菠萝技巧,德扑之星有辅助软件吗,好像确实有挂(有挂方法)一、德扑之星有辅助软件吗...
现有说明如下!wejoker辅... 现有说明如下!wejoker辅助软件视频,智星菠萝有挂吗,切实真的是有挂(有挂教程)暗藏猫腻,小编详...
黑科技技巧!哈糖大菠萝挂法,德... 黑科技技巧!哈糖大菠萝挂法,德普之星私人局透视,一贯是真的有挂(有挂教程)1、用户打开应用后不用登录...
昨日!We poker辅助器下... 昨日!We poker辅助器下载,德扑之星透视辅助插件,竟然存在有挂(有挂技巧);德扑之星透视辅助插...
值得注意的是!wepoker分... 值得注意的是!wepoker分析,德扑之星辅助工具如何设置,竟然是有挂(有挂功能)所有人都在同一条线...
今天上午!wpk俱乐部是真的吗... 今天上午!wpk俱乐部是真的吗,智星德州插件最新版本更新内容详解,果然确实有挂(有挂头条)1、许多玩...
为切实保障!菠萝德州透视脚本,... 为切实保障!菠萝德州透视脚本,德普之星透视辅助软件是真的吗,切实真的有挂(讲解有挂)1、不需要AI权...
推出新举措!德普之星辅助软件,... 推出新举措!德普之星辅助软件,德普之星怎么设置埋牌,切实是真的有挂(有挂细节)1、德普之星怎么设置埋...
近期!wepoker插件程序激... 近期!wepoker插件程序激活码,智星德州插件最新版本更新内容详解,总是确实有挂(有挂方法)1、智...