-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to remove null checks in source mappings? #225
Comments
Hello! Sounds like you've got a custom source->target member configuration where you're handling null-checking yourself and the built-in null-checking is getting in the way? Could you give me an example of what you're trying to map? Cheers, |
The mapping is between two classes named Address with mostly different properties. The target.AddressLine1 is populated with a lot of string.IsNullOrEmpty checks on the source properties. I don't have the code right now but here is the map that is produced:
As you can see, target.AddressLine1 is only composed if all the source properties are non-null. Also I notice that the if gains an else only if the properties have matching names. For example, County > County has an if-else but Town > PostalTown only has an if. Is that intentional? |
We have worked around it for now by using Before.CreatingTargetInstances.Call() to ensure each source property has a value but suspect there's a nicer way to deal with it. |
Hello! The null checks are the mapper protecting against null strings in the I suppose the proper solution would be a configuration option to indicate you're going to handle null-checking yourself, but (if I've understood the mapping correctly) - maybe this would be neater: Mapper.WhenMapping
.From<Source.Address>()
.To<Target.Address>()
.Map((sourceAddress, _) => string.Join(", ", new[]
{
sourceAddress.SubBuilding,
sourceAddress.Building,
sourceAddress.BuildingNumber,
sourceAddress.DependentThoroughfare,
sourceAddress.Thoroughfare
}.Where(addressPart => !string.IsNullOrWhiteSpace(addressPart))
.Select(addressPart => addressPart.Trim())))
.To(targetAddress => targetAddress.AddressLine1); Again unfortunately, testing this turned up a bug where the mapper falls over trying to figure out a null-check for Cheers, Steve |
If I map properties then the compiled plan says every source property value referenced in the Map() must not be null. How do I remove this null check?
In this instance I'm mapping an addressline1 from multiple source properties, but the plan is forcing all source properties to be non-null, which I clearly don't want as in the mapping I'm ignoring the null lines and concating the non-null.
The text was updated successfully, but these errors were encountered: