mstfcck
mstfcck

Reputation: 731

AutoMapper 8.0.0: How to ignore the map that if property has default value?

I have to class as follows:

public class ClassA
{
    public string Item1 { get; set; } // null
    public string Item2 { get; set; } // "This is not null."
    public int Item3 { get; set; } // 0
    public int Item4 { get; set; } // 1 (greater then 0)
}

public class ClassB
{
    public string Item1 { get; set; }
    public string Item2 { get; set; }
    public int Item3 { get; set; }
    public int Item4 { get; set; }
}

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<ClassA, ClassB>();
    }
}

I want to ignore the mapping which properties have default value in ClassA.

I have many classes in my project like this so I don't want to ignore separately every property. I need a global configuration for that.

I have read a lot of sample and spent 5 hours to do. I try to do with ForAllMembers but I couldn't do that.

What is the best way to do that?

Edit 1:

What I need that kind of thing?

I am using DevExtreme Grid and I am using edit popup on grid. In the popup, changed just two properties and saved. I am getting the as json string on "values" variable like example.

ClassA is a view model and have validation attributes. ClassB is a entity.

JsonConvert.PopulateObject(values, viewModel); I can map to the ClassA the json string. It is ok. But the other properties (Item1 and Item3) still have default values. That is why I want to ignore the properties have default values.

    [HttpPut]
    public IActionResult DxUpdate(int key, string values)
    {
        /*
            values example:
            {"item2":"This is not null.","item4":1}
         */

        var entity = _service.GetById(...);

        var viewModel = new ClassA();

        JsonConvert.PopulateObject(values, viewModel);

        if (!TryValidateModel(viewModel))
            return BadRequest(ModelState.GetFullErrorMessage());

        entity = _mapper.Map<ClassB>(viewModel);

        _service.Update(entity);

        return Ok();
    }

Edit 2:

ClassA have nullable int or double properties so if the properties are not nullable then haven't default values.

I am OK with an alternative solution. I didn't think of a good solution.

Upvotes: 0

Views: 785

Answers (1)

Flater
Flater

Reputation: 13773

Why do you need to ignore default properties?

If A.Item3 is the default 0 value (because it was never set to any other value), and you ignore the mapping of this property, B.Item3 is equal to 0 (because Automapper didn't set it and thus B.Item3 has the default int value); which is the exact same outcome as when Automapper does in fact copy over the default value 0 from A.Item3 to B.Item3.

What are you expecting to be different when you ignore the default properties?

There may be a reason to prevent this in cases where you are mapping between different property types, but your posted question does not contain such an example. Furthermore, in those cases you're generally going to be stuck having to create custom mappings anyway, which defeats your purpose of not having to write custom code for each mapping.

Upvotes: 1

Related Questions