Reputation: 11
I want to map 2 objects based on a condition, if true mapp else ignore, the condition is not a part of source neither destination
var mapperconfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForMember(source => source.Titulaires,
opt => opt.Condition(titulaires.HasValue && titulaires == true));
....
});
the extension method Condition() accepts just a type related to source or destination.
Upvotes: 1
Views: 1456
Reputation: 11
thank you all for your help, I find a way to test my conditions into my automapper configuration : MapperConfiguration mappConf = new MapperConfiguration(config => { config.CreateMap() .ForMember(destination => destination.member, option => option.Condition(item => _condition == true))........});
Upvotes: 0
Reputation: 606
AutoMapper allows you to add conditions to properties that must be met before that property will be mapped.
Eg.
public class Foo
{
public int baz;
}
public class Bar
{
public uint baz;
}
public class Program
{
public static void Main()
{
Mapper.CreateMap<Foo,Bar>().ForMember(dest => dest.baz, opt => opt.Condition(src => (src.baz >= 0)));
var foo1 = new Foo { baz=-1 };
var bar1 = Mapper.Map<Bar>(foo1);
Console.WriteLine("bar1.baz={0}", bar1.baz);
var foo2 = new Foo{ baz=100 };
var bar2 = Mapper.Map<Bar>(foo2);
Console.WriteLine("bar2.baz={0}", bar2.baz);
}
}
Also, they give Preconditions functionality
See this link Conditional Mapping
Upvotes: 1