4

Is it possible to configure AutoMapper to set all properties to default value in case if the source object is null for specified classes? I know that I should use Mapper.AllowNullDestinationValues = false; to do what I want for all classes in application. Here the sampled code that I use for tests, but it doesn't work

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

Using of this code:

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
dmolokanov
  • 41
  • 1
  • 3
  • Did you see this question?: http://stackoverflow.com/questions/3407838/automapper-create-instance-of-destination-type-if-source-null – Davin Tryon Sep 20 '12 at 13:00
  • Yes, I saw that topic, but i think that `Mapper.AllowNullDestinationValues = false;` and `Mapper.Configuration.AllowNullDestinationValues = false;` the same – dmolokanov Sep 20 '12 at 14:04
  • Could you explain, what this flag means, please? I can't find official documentation about it. – dmolokanov Sep 20 '12 at 14:10

2 Answers2

1

It must be related to "a" being already mapped.

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

All mappings are cached, so if you'll attempt to re-map same instance the automapper would just keep the original result.

In order to test it, try:

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

Here is a link to similar question.

Void Ray
  • 9,849
  • 4
  • 33
  • 53
0

Looks like it will replace with null if you set Mapper.Configuration.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.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; }
    }
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132