AutoMapper member-level transform not applied to destination value

329 views Asked by At

Quick question about member-level value transformers. I have a string property mapped to a decimal property. I need to scale and round the destination value after it is mapped. I thought a value transformer would be the appropriate way to do this.

CreateMap<Source, Target>()
  .ForMember(dest => dest.Rate, opts =>
  {
    opts.MapFrom(src => src.Rate);
    opts.AddTransform(x => Math.Round(x * 100M, 2));
  });

The value transformer never seems to be applied to the destination value (see fiddle, below for example)

I could also do

.ForMember(dest => dest.Rate, opts => opts.MapFrom(src => Math.Round(decimal.Parse(src.Rate) * 100, 2)));

but I'd prefer the member transform approach if possible as it uses AutoMapper's built-in string to decimal mapping convention not to mention it's a bit easier to read.

Am I misusing this feature? Any help or advice will be greatly appreciated!

Full example: https://dotnetfiddle.net/PxoDGY

Thanks for taking a look.

0

There are 0 answers