I'm trying to learn automapper (version 10.0.0) and I'm setting up my DTO input object to become my Entity Framework model object, so I did this:
CreateMap<RequestInputDTO, Request>()
.ForMember(x => x.RequestedById, x => x.Ignore())
.ForMember(x => x.RequestedForId, x => x.Ignore())
.ForMember(x => x.OrgHierarchyId, x => x.Ignore())
.AfterMap((src, dst) => {
dst.Uuid = Guid.NewGuid();
dst.RequestedUtc = DateTime.UtcNow;
});
And so far that part is working fine. However, for those three properties I told it to ignore, I have to do database lookups. They send me a name for example, I grab the ID of the corresponding record and then use that for the ID. So when I receive the request I'm doing this:
var request = _mapper.Map<Request>(dto);
request.RequestedById = await WwidToIdAsync(dto.RequestedByWwid);
request.OrgHierarchyId = await OrgHierarchyNumberToIdAsync(dto.RequestedForOrgHierarchyCode);
request.RequestedForId = await WwidToIdAsync(dto.RequestedForWwid);
While that seems to be doing the right thing, I don't like that I've now disconnected those three items from the automapping. It'd be too easy for someone to forget to do it. I can't figure out how to get the three lines handled automatically during the mapping since AfterMap isn't async.