Automapper:Hydrate int?基于条件
本文关键字:条件 于条件 Hydrate int Automapper | 更新日期: 2023-09-27 18:09:45
我有以下代码:
[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; }
}
如果值是null
,则基本上对int?
进行水合,除非是allowOverride = true
,在这种情况下,它将对值进行水合,而不检查字段的值。
我该如何在Automapper中执行此操作?
我知道你可以使用.CCondition((,如下所示:
自动映射程序';s条件被忽略
但我不知道如何:
- 应用基于
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();