【问题标题】:How to set up Automapper global options to allow destination values to be null when source is null如何设置 Automapper 全局选项以在源为空时允许目标值为空
【发布时间】:2023-01-28 15:14:57
【问题描述】:

我有下一个类,我想在其中使用 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&lt;PersonPatchTest, PersonTest&gt;(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】:

    这是在版本 12.0.1 中解决的错误。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多