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!
Use BeforeMap
method to set property value before mapping process:
Mapper.CreateMap<Source, Destination>()
.BeforeMap((s, d) => d.IsPropertyChangedEnabled = false );
You can also use ForMember() which has the added benefit of passing the standard unit test of Mapper.AssertConfigurationIsValid() when the properties being set to values are not in the source object.
here's an example
Mapper.CreateMap<ClientData, GenerateClientLetterCommand>()
.ForMember(x => x.Id, opt => opt.MapFrom( o => Guid.NewGuid()))
.ForMember(x => x.Created, opt => opt.MapFrom( o => DateTime.Now));
From what I understand from the description is that you don't want to fire the property change notification while fetch data from db using the DO and filling the BO.
One possible solution for this would be to have a base class for all BO having two major functionality, 1. Property - IsLoaded which will be set by the mapper after the data is loaded and 2. INotifyPropertyChange implementation and a method to wrap the RaisePropertyChange publisher to check the IsLoaded property and raise the event based on that.