1

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.

  • Transformers only apply when the source and the destination member types are the same. Here you need a type converter (global) or a value converter (per member). Also, the MapFrom is useless because the member names already match. – Lucian Bargaoanu Aug 25 '21 at 06:21
  • @LucianBargaoanu I'm also having the same problem, I thought this method was for additional transformation after value mapping ("Apply a transformation function after any resolved destination member value with the given type"), if that's not the case,maybe the docs/comments should be clearer... – Xriuk Aug 26 '22 at 14:47
  • It certainly does what it says on the tin, but we'll consider a PR if you feel things can be better. – Lucian Bargaoanu Aug 26 '22 at 16:12

0 Answers0