The difference is that PreCondition is executed before the initial value is reached, as well as by the preface of the condition, therefore, in this case, the predicate PreCondition will be executed before receiving the value from MyProperty, and then the value from the property is evaluated and, finally, the condition is satisfied.
In the following code you can see this
class Program { static void Main(string[] args) { Mapper.Initialize(cfg => { cfg.CreateMap<Person, PersonViewModel>() .ForMember(p => p.Name, c => { c.Condition(new Func<Person, bool>(person => { Console.WriteLine("Condition"); return true; })); c.PreCondition(new Func<Person, bool>(person => { Console.WriteLine("PreCondition"); return true; })); c.MapFrom(p => p.Name); }); }); Mapper.Instance.Map<PersonViewModel>(new Person() { Name = "Alberto" }); } } class Person { public long Id { get; set; } private string _name; public string Name { get { Console.WriteLine("Getting value"); return _name; } set { _name = value; } } } class PersonViewModel { public string Name { get; set; } }
Exiting this program:
PreCondition Getting value Condition
Because the Condition method contains an overload that receives an instance of ResolutionContext that has a property named SourceValue , the condition evaluates the value of the property from the source to set the SourceValue property in the ResolutionContext.
ATTENTION:
This behavior works correctly before version <= 4.2.1 and> = 5.2.0.
Versions between 5.1.1 and 5.0.2, the behavior no longer works properly.
The output in these versions:
Condition PreCondition Getting value
Alberto monteiro
source share