Configuration Validation¶
Hand-rolled mapping code, though tedious, has the advantage of being testable. One of the inspirations behind AutoMapper was to eliminate not just the custom mapping code, but eliminate the need for manual testing. Because the mapping from source to destination is convention-based, you will still need to test your configuration.
AutoMapper provides configuration testing in the form of the AssertConfigurationIsValid method. Suppose we have slightly misconfigured our source and destination types:
public class Source
{
public int SomeValue { get; set; }
}
public class Destination
{
public int SomeValuefff { get; set; }
}
var configuration = new MapperConfiguration(cfg =>
cfg.CreateMap<Source, Destination>());
configuration.AssertConfigurationIsValid();
Overriding configuration errors¶
To fix a configuration error (besides renaming the source/destination members), you have three choices for providing an alternate configuration:
- Custom Value Resolvers
- Projection
- Use the Ignore() option
With the third option, we have a member on the destination type that we will fill with alternative means, and not through the Map operation.
var configuration = new MapperConfiguration(cfg =>
cfg.CreateMap<Source, Destination>()
.ForMember(dest => dest.SomeValuefff, opt => opt.Ignore())
);
Selecting members to validate¶
By default, AutoMapper uses the destination type to validate members. It assumes that all destination members need to be mapped. To modify this behavior, use the CreateMap overload to specify which member list to validate against:
var configuration = new MapperConfiguration(cfg =>
cfg.CreateMap<Source, Destination>(MemberList.Source);
cfg.CreateMap<Source2, Destination2>(MemberList.None);
);
To skip validation altogether for this map, use MemberList.None. That's the default for ReverseMap.
Custom validations¶
You can add custom validations through an extension point. See here.