我有以下代码:
[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
基本上保留int?
,其中它将水合值而不检查字段的值。
我将如何在Automapper中执行此操作?
我知道您可以使用.Condition(),如下所示:
Automapper's condition gets ignored
但我无法弄清楚如何:
allowOverride
应用逻辑,而不是逐个定义。答案 0 :(得分: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();
现在将检查值的目标,并根据构造函数参数覆盖它。
或强>
如果您希望能够为每个映射专门配置它,那么您可以使用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();