How To Filter Mapped Records Generically

9 views
Skip to first unread message

group...@gmail.com

unread,
Nov 7, 2016, 3:48:47 PM11/7/16
to AutoMapper-users
I have a complex object type with multiple sub objects. The Parent Object contains information about multiple participants.  However, when one participant get a copy of the parent object information regarding the other participants should not be returned.  In the example below I solved the problem with with "ConvertUsing" which is a heavy handed solution that defeats all of the benefits of Automapper.  I am looking for a more generic solution.

The following code was returning objects that included user information that I don't want to share.

cfg.CreateMap<GroupEventServiceRequest, GroupEvent_ServiceRequestResponsePartial>()
                        .ForMember(x => x.userObjectId, opt => opt.MapFrom(src => src.SeatUser.objectId  ))
                        .ForMember(x => x.serviceObjectId, opt => opt.MapFrom(src => src.HostService.objectId  ));

I solved it with:

cfg.CreateMap<GroupEventServiceRequest, GroupEvent_ServiceRequestResponsePartial>().ConvertUsing( new AutoMapper_GetGesr_UserLimited());

But what I want is something more akin to:

cfg.CreateMap<GroupEventServiceRequest, GroupEvent_ServiceRequestResponsePartial>( c => c.src.userid == userid )
                        .ForMember(x => x.userObjectId, opt => opt.MapFrom(src => src.SeatUser.objectId  ))
                        .ForMember(x => x.serviceObjectId, opt => opt.MapFrom(src => src.HostService.objectId  ));

Reply all
Reply to author
Forward
0 new messages