stackoverfloweth
stackoverfloweth

Reputation: 6917

AutoMapper PreCondition that checks destination

Steps:

  1. Receive ViewModel to update or create entity
  2. Attempt to get existing entity if it exists
  3. Call IMapper.map(viewModel, entity)

Because I'm providing a destination value to the map, is it possible to check that destination value for PreConditions?

Something like

.ForMember(x => x.Id, opt => {
   opt.PreCondition((src, desintation) => destination.Id == null);
   opt.MapFrom(src => Guid.NewGuid());
}

Is that possible? Maybe there's a better approach?

Upvotes: 2

Views: 2058

Answers (1)

stackoverfloweth
stackoverfloweth

Reputation: 6917

Found it

.ForMember(dest => dest.EditedBy, opt => {
    opt.PreCondition((src, dest, context) => dest.Id != Guid.Empty);
    opt.MapFrom(src => Guid.NewGuid());
});

Upvotes: 2

Related Questions