Automapper:水合物诠释? 根据条件(Automapper: Hydrate int?

2019-08-31 10:03发布

我有以下代码:

[Test]
public void ConditionalMapping()
{
    var src = new Sample1 {Age = 1, Number = null};
    var dest = new Sample2 {Age = null, Number = 1};

    Hydrate(src, dest, false);

    Assert.That(dest.Age, Is.EqualTo(1));
    Assert.That(dest.Number, Is.EqualTo(1));

    src = new Sample1 {Age = null, Number = 1};
    dest = new Sample2 {Age = 1, Number = null};

    Hydrate(src, dest, true);

    Assert.That(dest.Age, Is.Null);
    Assert.That(dest.Number, Is.EqualTo(1));
}

public void Hydrate(Sample1 src, Sample2 dest, bool allowOverride)
{
    if (!dest.Age.HasValue || allowOverride)
        dest.Age = src.Age;

    if (!dest.Number.HasValue || allowOverride)
        dest.Number = src.Number;
}

public class Sample1
{
    public int? Age { get; set; }
    public int? Number { get; set; }
}

public class Sample2
{
    public int? Age { get; set; }
    public int? Number { get; set; }
}

这基本上滋润的int? 如果该值是null除非allowOverride = true ,其中将水合值而不检查字段的值。

我怎么会去在Automapper这样做呢?

我知道,你可以使用.Condition(),如下所示:

Automapper的病情被忽略

但我无法弄清楚如何:

  1. 应用基于逻辑int? 不被一个定义它之一。
  2. 包括allowOverride布尔的映射。

Answer 1:

我可能已经找到了你潜在的解决方案-这取决于你如何想allowOverride标志工作。

如果你想在标志操作同样为所有映射您可以创建一个TypeConverter如下

public class NullableIntConverter : ITypeConverter<int?, int?>
{
    private bool AllowOverrides { get; set;}

    public NullableIntConverter(bool allowOverrides)
    {
        AllowOverrides = allowOverrides;
    }

    public int? Convert(ResolutionContext context)
    {
        var source = context.SourceValue as int?;
        var destination = context.DestinationValue as int?;
        if (destination.HasValue && !AllowOverrides)
            return destination;
        else
            return source;
    }
}

初始化这样的:

Mapper.CreateMap<Sample1, Sample2>();
Mapper.CreateMap<int?, int?>().ConvertUsing(new NullableIntConverter(true));
Mapper.AssertConfigurationIsValid();

现在将检查目的地的值,并根据您的构造函数的参数覆盖它适当。


要么


如果你希望能够专门配置它为每个映射,那么你可以使用一个ValueResolver (注意,此代码可以用一些额外的验证做):

public class NullableIntResolver : IValueResolver
{
    public bool AllowOverrides { get; set; }

    public NullableIntResolver(bool allowOverrides)
    {
        AllowOverrides = allowOverrides;
    }

    public ResolutionResult Resolve(ResolutionResult source)
    {
        // Add validation for source and destination types
        return source.New(
                   ResolveCore((int?) source.Value,
                               DestinationMemberValue(source.Context)),
                   typeof(int?));
    }

    public int? ResolveCore(int? source, int? destination)
    {
        if (destination.HasValue && !AllowOverrides)
            return destination;
        else
            return source;
    }

    private int? DestinationMemberValue(ResolutionContext context)
    {
        var destObject = context.DestinationValue;
        var destMemberName = context.MemberName;
        return (int?) destObject
                          .GetType()
                          .GetProperty(destMemberName)
                          .GetValue(destObject, null);
    }
}

然后,您可以初始化你的映射是这样的:

var allowOverrides = true;
Mapper.CreateMap<Sample1, Sample2>()
    .ForMember(dest => dest.Age,
               opt => opt.ResolveUsing<NullableIntResolver>()
               .FromMember(src => src.Age)
               .ConstructedBy(() => new NullableIntResolver(allowOverrides)))
    .ForMember(dest => dest.Number,
               opt => opt.ResolveUsing<NullableIntResolver>()
               .FromMember(src => src.Number)
               .ConstructedBy(() => new NullableIntResolver(allowOverrides)));
Mapper.AssertConfigurationIsValid();


文章来源: Automapper: Hydrate int? based on conditions