我有以下代码:
[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的病情被忽略
但我无法弄清楚如何:
- 应用基于逻辑
int?
不被一个定义它之一。 - 包括
allowOverride
布尔的映射。
我可能已经找到了你潜在的解决方案-这取决于你如何想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();