开发者

AutoMapper catches and ignores NullReferenceException

开发者 https://www.devze.com 2023-04-03 09:47 出处:网络
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.

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&开发者_如何学Cgt;(() => 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;
    }
}


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消