0

我有以下代码:

[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?如果值为nullunless ,它基本上allowOverride = true会水合一个值,其中它将在不检查字段值的情况下水合该值。

我将如何在 Automapper 中执行此操作?

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

Automapper 的条件被忽略

但我不知道如何:

  1. 应用基于的逻辑,int?无需一一定义。
  2. allowOverride将布尔值包含到 Mapper。
4

1 回答 1

0

我可能已经为您找到了一个潜在的解决方案 - 取决于您希望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();

它现在将检查目标值,并根据您的构造函数参数酌情覆盖它。


或者


如果您希望能够为每个映射专门配置它,那么您可以使用 a 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();
于 2013-04-18T03:28:53.233 回答