Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoMapper: manually set property

Tags:

c#

automapper

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!

like image 883
user2145393 Avatar asked Mar 07 '13 17:03

user2145393


People also ask

Is AutoMapper faster than manual mapping?

Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping.

How do I ignore property mapping in AutoMapper?

So, the AutoMapper Ignore() method is used when you want to completely ignore the property in the mapping. The ignored property could be in either the source or the destination object.

Does AutoMapper map private properties?

By default, AutoMapper only recognizes public members. It can map to private setters, but will skip internal/private methods and properties if the entire property is private/internal.


1 Answers

Use BeforeMap method to set property value before mapping process:

Mapper.CreateMap<Source, Destination>()       .BeforeMap((s, d) => d.IsPropertyChangedEnabled = false ); 
like image 74
Sergey Berezovskiy Avatar answered Sep 21 '22 07:09

Sergey Berezovskiy