ForAllOtherMembers extension method was removed from Automapper 11 I use it to ignore conventional mappings for properties other than the one mentioned before like this
ForAllOtherMembers(opt=>opt.ignore())
How to do this in Automapper 11 ?
ForAllOtherMembers extension method was removed from Automapper 11 I use it to ignore conventional mappings for properties other than the one mentioned before like this
ForAllOtherMembers(opt=>opt.ignore())
How to do this in Automapper 11 ?
I will never ever change even a single line in my code simply because the authors of AutoMapper decided that its not a "right" thing to do for whatever "reason".
Quick and dirty solution, makes sense to add a unit test:
using AutoMapper.Internal;
using AutoMapper.Configuration;
public static class AutoMapperExtensions
{
private static readonly PropertyInfo TypeMapActionsProperty = typeof(TypeMapConfiguration).GetProperty("TypeMapActions", BindingFlags.NonPublic | BindingFlags.Instance);
// not needed in AutoMapper 12.0.1
private static readonly PropertyInfo DestinationTypeDetailsProperty = typeof(TypeMap).GetProperty("DestinationTypeDetails", BindingFlags.NonPublic | BindingFlags.Instance);
public static void ForAllOtherMembers<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression, Action<IMemberConfigurationExpression<TSource, TDestination, object>> memberOptions)
{
var typeMapConfiguration = (TypeMapConfiguration)expression;
var typeMapActions = (List<Action<TypeMap>>)TypeMapActionsProperty.GetValue(typeMapConfiguration);
typeMapActions.Add(typeMap =>
{
var destinationTypeDetails = (TypeDetails)DestinationTypeDetailsProperty.GetValue(typeMap);
foreach (var accessor in destinationTypeDetails.WriteAccessors.Where(m => typeMapConfiguration.GetDestinationMemberConfiguration(m) == null))
{
expression.ForMember(accessor.Name, memberOptions);
}
});
}
}
You can set this on the CreateMap call:
CreateMap<TSource, TDest>(MemberList.None)
MemberList.None does not prevent auto-mapped properties with the same member names.
I also tried some other solutions to find the map for the given profile and change the property map to ignore for unmapped property names, however this did not work since the properties were already considered to be mapped.
The unfortunate answer to this problem for me was to use a CustomTypeConverter
public class OrderTypeConverter : ITypeConverter<ThirdPartyOrder, MyOrder>
{
public Order.Order Convert(ThirdPartyOrder source, MyOrder destination, ResolutionContext context) =>
new MyOrder()
{
id = source.id,
__type = source.__type,
company_id = source.company_id,
stops = source.stops
};
}
Then
private readonly OrderTypeConverter orderTypeConverter;
public OrderProfile()
{
this.orderTypeConverter = new OrderTypeConverter();
this.CreateMap<ThirdPartyOrder, MyOrder>().ConvertUsing(orderTypeConverter);
}