Permissive mappings of unspecified properties

18 views
Skip to first unread message

Michael Powell

unread,
May 1, 2016, 3:54:51 PM5/1/16
to automapp...@googlegroups.com
Hello,

I think I am seeing a permissive condition going on mapping
unspecified properties.

What I would expect is that if the properties have not been specified,
the mapper should not proceed with the mapping. In other words, in a
declarative, not permissive, manner.

In my case, I've got a parent class mapping just the Name (and a
couple of other bits) from a saving view model.

I've got some children mapped separately, intentionally, which should
be performed in a separate step.

However, what I am seeing is that the unspecified mappings are happening anyway.

I'm going to look at providing some conditions to block this behavior,
ignore the destination properties, etc, but any insights would be
appreciated.

Regards,

Michael Powell

Michael Powell

unread,
May 1, 2016, 4:01:24 PM5/1/16
to automapp...@googlegroups.com
For now, going with this seems to work, but if there's a better way,
interested to hear about it.

//...
.ForMember(d => d.Children, o => o.Ignore())
//...

> Regards,
>
> Michael Powell

Peter H

unread,
May 2, 2016, 1:39:11 AM5/2/16
to AutoMapper-users
Isn't that where the auto in AutoMapper comes from? Not having to specify every members is what makes AutoMapper awesome!

Keith Barrows

unread,
May 2, 2016, 3:51:08 PM5/2/16
to AutoMapper-users
That is how I understand it and have used it since the 1.0 days.  AutoMapper will map any matching (type & name) properties.

When I want to have properties ignored I use this syntax:
CreateMap<AssemblyViewModel, WorkSessionViewModel>()
    .ForMember(dest => dest.Id, opts => opts.MapFrom(src => src.UserWorkSessionId))
    .ForMember(dest => dest.Status, opts => opts.MapFrom(src => (int) src.UserWorkSessionStatus))
    .Ignore(r => r.GlobalProgramSelectedList)
    .Ignore(r => r.GlobalCiiSelectedList)
    .Ignore(r => r.CreatedDate)
    .Ignore(r => r.ModifiedDate);

...where .Ignore() is an extension method:
internal static IMappingExpression<TSource, TDestination> Ignore<TSource, TDestination>(this IMappingExpression<TSource, TDestination> map, Expression<Func<TDestination, object>> selector)
{
    map.ForMember(selector, config => config.Ignore());
    return map;
}

Michael Powell

unread,
May 2, 2016, 7:35:09 PM5/2/16
to AutoMapper-users


On Monday, May 2, 2016 at 1:39:11 AM UTC-4, Peter H wrote:
Isn't that where the auto in AutoMapper comes from? Not having to specify every members is what makes AutoMapper awesome!  

It makes sense; it's good to know there is an opt out for those special/corner cases. 
Reply all
Reply to author
Forward
0 new messages