Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Seems that the [AdaptIgnore] attribute does not work as expected!

Open moghimi opened this issue 3 years ago • 1 comments

I have a base class with some control fields which are filled at the construction or they will fetch from the related table from database when I use EF. I added the [AdaptIgnore] attribute to these properties in order to avoid changing them during data update, but when I use Adapt<Destination> , all base properties will change even if the desired members do not exist in the source class.

To test this issue, I created the following code. As you see in the output, all audit properties from base class will regenerated after mapping while I just want to change the Name property.

using Mapster;
using System.Text.Json;

public abstract class Base
{
    [AdaptIgnore]
    public DateTime CreatedOn { get; private set; }

    [AdaptIgnore]
    public DateTime UpdatedOn { get; set; }

    [AdaptIgnore]
    public int State { get; set; }

    public Base()
    {
        CreatedOn = DateTime.UtcNow;
        UpdatedOn = DateTime.UtcNow;
        State = 1;
    }
}

public class POCO : Base
{
    public string Name { get; set; }
}

public class Dto
{
    public string Name { get; set; }
}

internal class Program
{
    static void Main(string[] args)
    {
        var destination = new POCO() { Name = "Destination", State = 2 };
        var source = new Dto() { Name = "Source"};

        Console.WriteLine($"Before mapping: {JsonSerializer.Serialize(destination)}");
        Console.WriteLine($"Mapping...");
        Thread.Sleep(1000);
        destination = source.Adapt<POCO>();
        Console.WriteLine($"After mapping: {JsonSerializer.Serialize(destination)}");
    }
}

output:

Before mapping: {"Name":"Destination","CreatedOn":"2022-06-24T20:00:58.1942909Z","UpdatedOn":"2022-06-24T20:00:58.1943397Z","State":2}

Mapping...

After mapping: {"Name":"Source","CreatedOn":"2022-06-24T20:00:59.3039894Z","UpdatedOn":"2022-06-24T20:00:59.3039895Z","State":1}

moghimi avatar Jun 24 '22 20:06 moghimi

Any suggestions?

moghimi avatar Aug 06 '22 08:08 moghimi

Hi @moghimi, may I ask which version of Mapster you are using?

andrerav avatar Jan 03 '23 22:01 andrerav

@moghimi I am struggling to understand the problem here. Did you actually mean to do:

destination = source.Adapt(destination);

and not:

destination = source.Adapt<POCO>();

Because the latter will simply create a new POCO, map the source object to it, and overwrite the destination object with this new POCO.

andrerav avatar Jan 07 '23 14:01 andrerav

Hi @moghimi, may I ask which version of Mapster you are using?

Hi @andrerav , It was a long time ago, but I think it was 7.3.0

moghimi avatar Jan 07 '23 20:01 moghimi

        destination = source.Adapt<POCO>();

I used the latter one, so in this case [AdaptIgnore] attribute will be ignored?

moghimi avatar Jan 07 '23 20:01 moghimi

No, but your code isn't doing what you think it does. You are overwriting destination with a completely new POCO object mapped from source. Please use destination = source.Adapt(destination); if you want to map from source to destination.

andrerav avatar Jan 07 '23 23:01 andrerav