AutoMapper: manually set property

user2145393 picture user2145393 · Mar 7, 2013 · Viewed 29.2k times · Source

I am using AutoMapper to map from flat DataObjects to fat BusinessObjects and vice versa. I noticed that mapping from DataObjects to BusinessObjects takes extra time because of change notification of the BusinessObjects (implements INotifyPropertyChanged with custom validation, etc).

Because I normally don't need change notification during mapping, I'd like to turn it off. So I added a property "IsPropertyChangedEnabled". If this property is set to false, no NotifyPropertyChanged event is not raised and time is saved.

Question:

Can I tell AutoMapper to set this property to false at the very beginning of the mapping process? If so, how?

Thank you!

Answer

Sergey Berezovskiy picture Sergey Berezovskiy · Mar 7, 2013

Use BeforeMap method to set property value before mapping process:

Mapper.CreateMap<Source, Destination>()
      .BeforeMap((s, d) => d.IsPropertyChangedEnabled = false );