简体   繁体   English

AutoMapper - Condition和PreCondition之间有什么区别

[英]AutoMapper - What's difference between Condition and PreCondition

Suppose a mapping using AutoMapper like bellow: 假设使用AutoMapper进行映射,如下所示:

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

What's difference of substitute Condition by PreCondition: Precondition的替代条件有何不同:

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

What's the practical difference between this two methods? 这两种方法之间的实际区别是什么?

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. 不同之处在于PreCondition在获取源值和Condition谓词之前执行,因此在这种情况下,在从MyProperty获取值之前,将运行PreCondition谓词,然后评估属性值并最终执行Condition。

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. 因为Condition方法包含一个接收ResolutionContext实例的重载,该实例具有一个名为SourceValue的属性,Condition会从source计算属性值,以在ResolutionContext对象上设置SourceValue属性。

ATTENTION: 注意:

This behavior work properly until version <= 4.2.1 and >= 5.2.0. 此行为适用于版本<= 4.2.1和> = 5.2.0。

The versions between 5.1.1 and 5.0.2, the behavior is not working properly anymore. 5.1.1和5.0.2之间的版本,行为不再正常工作。

The output in those versions is: 这些版本的输出是:

Condition
PreCondition
Getting value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM