Suppose a mapping using AutoMapper like bellow:
mapItem.ForMember(to => to.SomeProperty, from =>
{
from.Condition(x => ((FromType)x.SourceValue).Oth
The diference is that PreCondition is executed before acessing the source value and also the Condition predicate, so in this case, before get the value from MyProperty the PreCondition predicate will run, and then the value from property is evaluated and finally Condition is executed.
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; }
}
The output from this program is:
PreCondition
Getting value
Condition
Because the Condition method contains a overload that receives a ResolutionContext instance, that have a property called SourceValue, the Condition evaluate the property value from source, to set the SourceValue property on ResolutionContext object.
This behavior work properly until version <= 4.2.1 and >= 5.2.0.
The versions between 5.1.1 and 5.0.2, the behavior is not working properly anymore.
The output in those versions is:
Condition
PreCondition
Getting value