Gean Ribeiro Gean Ribeiro - 1 year ago 177
C# Question

AutoMapper - What's difference between Condition and PreCondition

Suppose a mapping using AutoMapper like bellow:

mapItem.ForMember(to => to.SomeProperty, from =>
from.Condition(x => ((FromType)x.SourceValue).OtherProperty == "somethink");
from.MapFrom(x => x.MyProperty);

What's difference of substitute Condition by PreCondition:

from.PreCondition(x => ((FromType)x.SourceValue).OtherProperty == "somethink");

What's the practical difference between this two methods?

Answer Source

The difference is when the condition is applied.

The VS Magazine has a article about Automapper:

Pre and Post Mapping Actions AutoMapper will also let you define code to run before and after the mapping process. The pre-condition is useful if you have a property on your source object that you want to use in the mapping, but it has to be loaded or initialized before it can be used (perhaps loaded from NHibernate). For this type of situation,... continue to article: Simplify Your Projections with AutoMapper