Automapper v5 Ignore non-displayable properties

Previously, when I used Automapper v3.x, ignoring unlabeled properties could be done simply by adding the .IgnoreUnmappedProperties() extension, which looked like this:

 public static class AutoMapperExtensions { public static IMappingExpression<TSource, TDestination> IgnoreUnmappedProperties<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression) { var typeMap = Mapper.FindTypeMapFor<TSource, TDestination>(); if (typeMap != null) { foreach (var unmappedPropertyName in typeMap.GetUnmappedPropertyNames()) { expression.ForMember(unmappedPropertyName, opt => opt.Ignore()); } } return expression; } } 

How this extension can be rewritten to work with version 5.x. Of course, I can add the following to each property.

 .ForMember(dest => dest.LastUpdatedBy, opt => opt.Ignore()) 

or not cause

 Mapper.AssertConfigurationIsValid(); 
+5
source share
1 answer

This can be done using the new CreateMap method method and specify the required check.

 CreateMap<TSource, TDestination>(MemberList.None) 

MemberList.None should do the trick. You can also switch between source or destination checks.

Automapper - selection of participants for verification

+4
source

Source: https://habr.com/ru/post/1258014/


All Articles