AutoMapper 捕获并忽略 NullReferenceException

发布于 2024-12-03 11:37:44 字数 2435 浏览 1 评论 0原文

也许这是设计使然,但我们最初并不希望自动映射器捕获并忽略映射中的所有 NullReferenceException。我们主要使用 MapFrom 并有时创建复杂的表达式。我们希望这些映射在出现任何异常(甚至是 NullReferenceException)时失败,但我们无法让 AutoMapper 做到这一点。有没有办法让自动映射器不忽略所有这些异常,而不必为每种情况编写自定义值解析器?这对我们来说意味着大量额外的代码,事实上,如果不首先使用 automapper,代码可能会更少。

这些是我们期望全部通过的测试:

[TestFixture]
public class Tests
{
    [SetUp]
    public void Setup() { Mapper.Reset(); }

    [Test]
    public void ShouldThrowMappingExceptionUsingMapFromExpression()
    {
        Mapper.CreateMap<Source, Destination>()
            .ForMember(d => d.DestinationMember, o => o.MapFrom(s => s.SourceMember.SourceProperty))
            ;

        Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
    }

    [Test]
    public void ShouldThrowMappingExceptionUsingResolveUsingExpression()
    {
        Mapper.CreateMap<Source, Destination>()
            .ForMember(d => d.DestinationMember, o => o.ResolveUsing(s => s.SourceMember.SourceProperty))
            ;

        Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
    }

    [Test]
    public void ShouldThrowMappingExceptionUsingResolverInstance()
    {
        Mapper.CreateMap<Source, Destination>()
            .ForMember(d => d.DestinationMember, o => o.ResolveUsing(new TestValueResolver()).FromMember(s => s.SourceMember))
            ;

        Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
    }

    [Test]
    public void ShouldThrowMappingExceptionUsingResolverType()
    {
        Mapper.CreateMap<Source, Destination>()
            .ForMember(d => d.DestinationMember, o => o.ResolveUsing<TestValueResolver>().FromMember(s => s.SourceMember))
            ;

        Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
    }

}

public class Destination
{
    public string DestinationMember { get; set; }
}

public class Source
{
    public SourceChild SourceMember { get; set; }
}

public class SourceChild
{
    public string SourceProperty { get; set; }
}

public class TestValueResolver : ValueResolver<SourceChild, string>
{
    protected override string ResolveCore(SourceChild source)
    {
        return source.SourceProperty;
    }
}

Maybe this is by design, but we initially did not expect automapper to catch and ignore all NullReferenceExceptions in our mappings. We mostly use the MapFrom and create sometimes complex expressions. We want these mappings to fail if there's any exception, even a NullReferenceException, but we cant get AutoMapper to do that. Is there any way to make automapper not ignore all these exceptions without having to write a custom value resolver for every case? This would mean alot of extra code for us, so much in fact that it probably would be less code without using automapper in the first place.

These are the test that we would expect to all pass:

[TestFixture]
public class Tests
{
    [SetUp]
    public void Setup() { Mapper.Reset(); }

    [Test]
    public void ShouldThrowMappingExceptionUsingMapFromExpression()
    {
        Mapper.CreateMap<Source, Destination>()
            .ForMember(d => d.DestinationMember, o => o.MapFrom(s => s.SourceMember.SourceProperty))
            ;

        Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
    }

    [Test]
    public void ShouldThrowMappingExceptionUsingResolveUsingExpression()
    {
        Mapper.CreateMap<Source, Destination>()
            .ForMember(d => d.DestinationMember, o => o.ResolveUsing(s => s.SourceMember.SourceProperty))
            ;

        Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
    }

    [Test]
    public void ShouldThrowMappingExceptionUsingResolverInstance()
    {
        Mapper.CreateMap<Source, Destination>()
            .ForMember(d => d.DestinationMember, o => o.ResolveUsing(new TestValueResolver()).FromMember(s => s.SourceMember))
            ;

        Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
    }

    [Test]
    public void ShouldThrowMappingExceptionUsingResolverType()
    {
        Mapper.CreateMap<Source, Destination>()
            .ForMember(d => d.DestinationMember, o => o.ResolveUsing<TestValueResolver>().FromMember(s => s.SourceMember))
            ;

        Assert.Throws<AutoMapperMappingException>(() => Mapper.Map<Source, Destination>(new Source()));
    }

}

public class Destination
{
    public string DestinationMember { get; set; }
}

public class Source
{
    public SourceChild SourceMember { get; set; }
}

public class SourceChild
{
    public string SourceProperty { get; set; }
}

public class TestValueResolver : ValueResolver<SourceChild, string>
{
    protected override string ResolveCore(SourceChild source)
    {
        return source.SourceProperty;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2024-12-10 11:37:44

此行为现​​已在 AutoMapper 中更改! :-)

https://github.com/AutoMapper/AutoMapper/issues/122

This behaviour has now been changed in AutoMapper! :-)

https://github.com/AutoMapper/AutoMapper/issues/122

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文