如何设置 Automapper 全局选项以允许当源为空时目标值为空

问题描述 投票:0回答:1

我想在下一个类中使用 Automapper,如果源属性为 null,则也将目标属性设置为 null:

public class CompanyTest
{
    public Guid? Id { get; set; }
    public string CompanyName { get; set; } = string.Empty;
    public PersonTest? CEO { get; set; }
    public List<PersonTest> People { get; set; } = new();
}

public class CompanyPatchTest
{
    public string? CompanyName { get; set; }
    public PersonPatchTest? CEO { get; set; }
    public List<PersonPatchTest>? People { get; set; }
}

public class PersonTest
{
    public Guid? Id { get; set; }
    public string Name { get; set; }
    public List<PersonTest> Children { get; set; } = new();
}

public class PersonPatchTest
{
    public string? Name { get; set; }
    public List<PersonPatchTest>? Children { get; set; }
}

我创建了下一个失败的 xunit 测试:

using Xunit;
using AutoMapper;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;

namespace UnitTests.Tests;

public class AutomapperTests
{
    private readonly IMapper _mapper;

    public AutomapperTests()
    {
        var services = new ServiceCollection();

        services.AddAutoMapper(x =>
        {
            x.AddProfile(new ProfileTest());
        });

        var serviceProvider = services.BuildServiceProvider();
        _mapper =  serviceProvider.GetRequiredService<IMapper>();
    }

    [Fact]
    public void ShouldSetNullValue()
    {
        var company = new CompanyTest
        {
            Id = Guid.NewGuid(),
            CompanyName = "OriginalName",
            CEO = new()
            { 
                Id = Guid.NewGuid(),
                Name= "Name",
                Children = new()
                {
                    new PersonTest
                    {
                        Id = Guid.NewGuid(),
                        Name= "Name",
                    }
                }
            },
        };

        var source = new CompanyPatchTest
        {
            CompanyName = "CompanyName",
            CEO = null,
        };

        company = _mapper.Map<CompanyPatchTest, CompanyTest>(source, company);
        company.CEO.Should().BeNull();
    }
}

我的个人资料是下一篇:

public class ProfileTest : Profile
{
    public ProfileTest()
    {
        CreateMap<CompanyTest, CompanyPatchTest>();
        CreateMap<PersonTest, PersonPatchTest>();

        CreateMap<CompanyPatchTest, CompanyTest>(MemberList.Source)
            .ForMember(d => d.CEO, op => op.AllowNull());

        CreateMap<PersonPatchTest, PersonTest>(MemberList.Source);
    }
}

如果我删除

CreateMap<PersonPatchTest, PersonTest>(MemberList.Source);
线,那么它就可以工作。但我无法自定义该映射。

此外,如果可以将其设置为所有属性的全局设置,那就太好了。

我尝试过使用

AllowNullDestinationValues
但它不起作用

...
public AutomapperTests()
    {
        var services = new ServiceCollection();

        services.AddAutoMapper(x =>
        {
            x.AddProfile(new ProfileTest());
            x.AllowNullCollections = true;
            x.AllowNullDestinationValues = true;
        });

        var serviceProvider = services.BuildServiceProvider();
        _mapper =  serviceProvider.GetRequiredService<IMapper>();
    }
...

如果使用 Automapper 无法完成此操作,您知道还有其他工具可以实现此目的吗?

自动映射器版本:12.0.0

c# automapper
1个回答
1
投票

这是一个错误,已在版本 12.0.1 中解决。您不需要任何设置即可通过该测试。

AllowNullDestinationValues
的默认值为
true

https://github.com/AutoMapper/AutoMapper/pull/4083

© www.soinside.com 2019 - 2024. All rights reserved.