AutoMapper-条件和前提条件有什么区别 [英] AutoMapper - What's difference between Condition and PreCondition

查看:365
本文介绍了AutoMapper-条件和前提条件有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设像下面这样使用AutoMapper进行映射:

Suppose a mapping using AutoMapper like bellow:

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:

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

这两种方法的实际区别是什么?

What's the practical difference between this two methods?

推荐答案

不同之处在于PreCondition是在获取源值和Condition谓词之前执行的,因此,在这种情况下,是在从 MyProperty PreCondition谓词将运行,然后评估来自property的值并最终执行Condition.

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; }
}

该程序的输出为:

PreCondition
Getting value
Condition

由于Condition方法包含一个重载,该重载接收具有一个名为 SourceValue 的属性的ResolutionContext实例,因此Condition从源评估该属性值,以在ResolutionContext对象上设置SourceValue属性.

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.

直到版本< = 4.2.1和> = 5.2.0为止,此行为才能正常工作.

This behavior work properly until version <= 4.2.1 and >= 5.2.0.

在5.1.1和5.0.2之间的版本,此行为不再正常工作.

The versions between 5.1.1 and 5.0.2, the behavior is not working properly anymore.

这些版本的输出为:

Condition
PreCondition
Getting value

这篇关于AutoMapper-条件和前提条件有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆