AutoMapper - NullSubstitution

Sometimes you may want to provide default values if there are null values for a member in the source. Automapper has got a NullSubstitute method in configuration to specify a value for a memeber having null value.

Syntax

NullSubstitute("<provide default value for null>"

Usage

var config = new MapperConfiguration(cfg =>
                {

                    cfg.CreateMap<ModelData, ViewModel>()
						.ForMember(dest => dest.Name, opt => opt.NullSubstitute("Default Name"));
                });

Example

In this example, I have provided null for the Name property. But since I have used the NullSubstitute method in the configuration for that member, the value provided there will be mapped to destination object


No Comments

Add a Comment