So, following the advice of this answer, I've set up a string to trimmed string map in my base AutoMapper (3.3.1) config, as:
configuration.CreateMap<string, string>().ConvertUsing<StringToTrimmedStringConverter>();
Which works great, except for exactly like two fields in my database which need to preserve whitespace for interoperability reasons. Is there anything I can do in my data model to entity mapping to preserve whitespace when mapping between two string fields? I've tried:
.ForMember(d => d.WhitespaceField,
opts => opts.ResolveUsing<WhitespaceStringResolver>
(m => m.WhitespaceModelField))
where WhitespaceStringResolver is just a no-op:
public class WhitespaceStringResolver : ValueResolver<string, string>
{
protected override string ResolveCore(string source)
{
return source;
}
}
but that's not working. I can see execution hit the resolver but the strings wind up trimmed anyway.
Is there some way to ignore the base config's mapping and not trim the strings, just for the couple of fields I explicitly want to ignore it for?
Upgrading AutoMapper version is not something I want to do at this time.
What worked out in the end was adding
.AfterMap((model, entity) =>
{ entity.WhitespaceField = model.WhitespaceModelField; })
I could have also marked entity.WhitespaceField ignored, if I didn't need a field-to-field mapping around for a library that does searching using the model-to-entity mappings by convention.