【问题标题】:AutoMapper: Set all properties of destination object to default if source object is null for specified typesAutoMapper:如果指定类型的源对象为空,则将目标对象的所有属性设置为默认值
【发布时间】:2012-09-12 19:56:15
【问题描述】:

如果指定类的源对象为空,是否可以配置 AutoMapper 将所有属性设置为默认值?我知道我应该使用Mapper.AllowNullDestinationValues = false; 为应用程序中的所有类做我想做的事情。 这是我用于测试的示例代码,但它不起作用

public class A
{
    static A()
    {
        Mapper.Initialize(
            config =>
                {
                    config.ForSourceType<B>().AllowNullDestinationValues = false;
                    config.CreateMap<B, A>()
                        .ForMember(member => member.Name, opt => opt.Ignore());
                });
        //Mapper.AllowNullDestinationValues = false;

        Mapper.AssertConfigurationIsValid();
    }

    public void Init(B b)
    {
        Mapper.DynamicMap(b, this);
    }

    public int? Foo { get; set; }
    public double? Foo1 { get; set; }
    public bool Foo2 { get; set; }
    public string Name { get; set; }
}

public class B
{
    public string Name { get; set; }
    public int? Foo { get; set; }
    public double? Foo1 { get; set; }
    public bool Foo2 { get; set; }
}

使用此代码:

var b = new B() {Foo = 1, Foo1 = 3.3, Foo2 = true, Name = "123"};
var a = new A {Name = "aName"};
a.Init(b);      // All ok: Name=aName, Foo=1, Foo1=3,3, Foo2=True
a.Init(null);   // Should be Name=aName, Foo=null, Foo1=null, Foo2=False, 
                // but a has the same values as on a previous line

【问题讨论】:

  • 你看到这个问题了吗?:stackoverflow.com/questions/3407838/…
  • 是的,我看到了那个话题,但我认为Mapper.AllowNullDestinationValues = false;Mapper.Configuration.AllowNullDestinationValues = false; 一样
  • 你能解释一下,这个标志是什么意思吗?我找不到有关它的官方文档。

标签: c# automapper automapper-2


【解决方案1】:

它必须与已经映射的“a”相关。

var a = new A {Name = "aName"};
a.Init(b);
a.Init(null);

所有映射都被缓存,因此如果您尝试重新映射同一个实例,自动映射器只会保留原始结果。

为了测试它,请尝试:

        var c = new A {Name = "x"};
        c.Init(null); 

这是一个link 类似问题。

【讨论】:

    【解决方案2】:

    如果您设置Mapper.Configuration.AllowNullDestinationValues = false,它看起来会替换为null:

    public class A
        {
            static A()
            {
                Mapper.Initialize(
                    config =>
                    {
                        config.ForSourceType<B>().AllowNullDestinationValues = false;
                        config.CreateMap<B, A>()
                            .ForMember(member => member.Name, opt => opt.Ignore());
                    });
                Mapper.Configuration.AllowNullDestinationValues = false;
    
                Mapper.AssertConfigurationIsValid();
            }
    
            public void Init(B b)
            {
                Mapper.DynamicMap(b, this);
            }
    
            public int? Foo { get; set; }
            public double? Foo1 { get; set; }
            public bool Foo2 { get; set; }
            public string Name { get; set; }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 2021-08-14
      • 2015-05-25
      • 2021-05-10
      相关资源
      最近更新 更多