Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

c# - AutoMapper 10 ValueTransformer not executed for value type destinations

When mapping empty strings to a double/integer/enum/... destination, an exception occurs. Unfortunately my source data have a lot of those empty strings which shall be transferred to correct values (in case of an empty string, the correct value is null in the destination model)

As I want to use the ProjectTo feature, I decided to use a ValueTransformer to convert the empty string to null first. However it seems that feature is seldomly used and bugged? Actually I had the intention to file an issue within the GitHub repo, but as I don't have signed the net-foundation-contribution-license-agreement (and it seems it currently not possible) I do as requested and post on Stack Overflow first.

Are there any workarounds?

string/Nullable double/enum/int/bool

    public class Source
    {
        public string Value { get; set; }
    }
    public class Dest
    {
        // public object Value { get; set; } // Use this line and the value transformer gets called
        public double? Value { get; set; }
    }

    public static void TextMapping()
    {
        var config = new MapperConfiguration(
            cfg =>
            {
                cfg.ValueTransformers.Add<string>(s => s == "" ? null : s);
                cfg.CreateMap<Source, Dest>();
            }
        );
        var mapper = config.CreateMapper();

        var source = new Source() { Value = null };
        var dest = mapper.Map<Dest>(source);
        Assert.Null(dest.Value);

        var source2 = new Source() { Value = "" };
        var dest2 = mapper.Map<Dest>(source2);
        Assert.Null(dest2.Value);
    }

Version: x.y.z

.NET 5.0 PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0"
PackageReference Include="AutoMapper" Version="10.1.1"

Expected behavior

In my source model, I've a lot of empty strings which shall be converted to Nullable structs (double, enum, bool, int, ...) When I have an empty string, I want to have an null value on my destination. Therefor I set up a value transformer: cfg.ValueTransformers.Add(s => s == "" ? null : s);

Actual behavior

It seems, the value transformer is not executed, when the DestinationMemberType is a struct (or some of those simple builtin types) When I change my DestinationMemberType to object, the ValueTransformer gets executed, though. When using non-Nullable values, the ValueTransformer is not executed as well, only on reference types as far as I can see.

Steps to reproduce

I've set up a dotnet Fiddle: https://dotnetfiddle.net/2baMGv


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...