Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper graceful handling of NULL

Tags:

automapper

I'm mapping a pretty deeply nested entity to a flattened Dto object and was wondering how I'd handle this gracefully with AutoMapper. I know I could null check each property as it's being mapped, but that's going to get VERY ugly for things like:

ForMember(s => s.Property, o => o.MapFrom(s => s.Parent.Child1.Child2.Child3.Property)

So I'm guess I could use various maps configs to the same destination object... but being relatively unskilled with AutoMapper I'm not sure what the performance implications of this are. What other, better, ways do I have of achieving what I want?

To re-iterate, I'm hoping to avoid something like this (I'm aware the below code probably doesn't compile... it's just an example) which I'd have to do for every member:

ForMember(s => s.Property, o => o.MapFrom(
    s => s.Parent == null ? string.Empty :
    s => s.Parent.Child1 == null ? string.Empty :
    s => s.Parent.Child1.Child2 == null ? string.Empty :
    s => s.Parent.Child1.Child2.Child3 == null ? string.Empty :
    s => s.Parent.Child1.Child2.Child3.Property));
like image 502
Paul Aldred-Bann Avatar asked Mar 24 '15 10:03

Paul Aldred-Bann


People also ask

How does AutoMapper handle null?

The Null substitution allows us to supply an alternate value for a destination member if the source value is null. That means instead of mapping the null value from the source object, it will map from the value we supply.

Why don t use AutoMapper?

AutoMapper automaps by name, and flattens properties, so there is no need to explicitly map names that already match. Doing so results in more code than mapping manually, making the use of AutoMapper pointless. Please don't. Use vertical slices instead.

Is it good to use AutoMapper?

If your objects are mapped with default Automapper config, you cannot find where a field takes its value. Even if you use good tooling (VS, Rider) and try to “Find usages” you won't be able to find neither assignment nor usage. This is especially bad for developers new to project.

What is the point of AutoMapper?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.


2 Answers

I think AutoMapper will actually handle null propagation like this automatically for you. Your example:

ForMember(s => s.Property, o => o.MapFrom(s => s.Parent.Child1.Child2.Child3.Property)

should resolve to null(I think), if any of the intermediate members are null.

Example: https://dotnetfiddle.net/hMo3wa

like image 147
Andrew Whitaker Avatar answered Sep 21 '22 23:09

Andrew Whitaker


While this works for individual instances, it does not work for Projections on queryable collections.

This additional code I added to Andrew's answer fails miserably, even on the latest, 5.1.1 version.

    var list = new List<Source>();
    list.Add(new Source());     
    list.AsQueryable().ProjectTo<Dest>().Dump();

Example https://dotnetfiddle.net/Ecovrp

like image 29
Victor Ortuondo Avatar answered Sep 18 '22 23:09

Victor Ortuondo