我目前在.net core(2.1)api项目中使用Automapper(8.0.0)。下面是我的代码示例,用于模拟dto映射到数据库对象的更新过程。我希望让自动映射器忽略Dto中空对象的映射。
using AutoMapper;
using System;
namespace testAutoMapper
{
public class SourceObject
{
public bool TestReverseFalse { get; set; }
public bool TestReverseTrue { get; set; }
public bool IFalse { get; set; }
public bool ITrue { get; set; }
public int TestInteger { get; set; }
public int TestNullInteger { get; set; }
}
public class ModelObject
{
public bool? TestReverseFalse { get; set; }
public bool? TestReverseTrue { get; set; }
public bool? IFalse { get; set; }
public bool? ITrue { get; set; }
public int? TestInteger { get; set; }
public int? TestNullInteger { get; set; }
}
public class Program
{
public static void Main(string[] args)
{
AutoMapper.Mapper.Initialize(mapper =>
{
//global ignore for all
//mapper.ForAllMaps((tm, me) => me.ForAllMembers(option => option.Condition((src, destination, sourceMember) => sourceMember != null)));
mapper.CreateMap<ModelObject, SourceObject>().ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));
});
var model = new ModelObject
{
TestReverseFalse = true,
TestReverseTrue = false,
};
var source = new SourceObject
{
TestReverseFalse = false,
TestReverseTrue = true,
ITrue = true,
IFalse = false,
TestInteger = 100,
TestNullInteger = 100
};
Console.WriteLine("Source Before Mapping");
Console.WriteLine("TestReverseFalse: " + source.TestReverseFalse);
Console.WriteLine("TestReverseTrue: " + source.TestReverseTrue);
Console.WriteLine("ITrue:" + source.ITrue);
Console.WriteLine("IFalse:" + source.IFalse);
Console.WriteLine("TestInteger:" + source.TestInteger);
Console.WriteLine("TestNullInteger:" + source.TestNullInteger);
Mapper.Map(model, source);
Console.WriteLine();
Console.WriteLine("After Mapping");
Console.WriteLine("TestReverseFalse: " + source.TestReverseFalse);
Console.WriteLine("TestReverseTrue: " + source.TestReverseTrue);
Console.WriteLine("ITrue:" + source.ITrue);
Console.WriteLine("IFalse:" + source.IFalse);
Console.WriteLine("TestInteger:" + source.TestInteger);
Console.WriteLine("TestNullInteger:" + source.TestNullInteger);
Console.ReadLine();
}
}
}
示例输出
Source Before Mapping
TestReverseFalse: False
TestReverseTrue: True
ITrue:True
IFalse:False
TestInteger:100
TestNullInteger:100
After Mapping
TestReverseFalse: True
TestReverseTrue: False
ITrue:False
IFalse:False
TestInteger:0
TestNullInteger:0
我发现我的源对象被重置为其默认值。 不确定我做错了哪一部分。
答案 0 :(得分:0)
使用自定义解析程序,该自定义解析程序将进行空检查以解决此问题。 将此替换为初始化:
foreach (var item in posts)
{
Console.WriteLine("providerName: " + item.ProviderName);
Console.WriteLine("providerIcon: " + item.providerIcon);
Console.WriteLine();
}
Mapper.Initialize(mapper =>
{
mapper.ForAllPropertyMaps(pm => true /*Whether to use this resolver. This returning true means it's always used*/, (pm, c) => c.MapFrom<NonNullOnlyResolver, object>(pm.SourceMember.Name));
mapper.CreateMap<ModelObject, SourceObject>();
});
更改后的输出:
class NonNullOnlyResolver : IMemberValueResolver<object, object, object, object>
{
public object Resolve(object source, object destination, object sourceMember, object destMember, ResolutionContext context)
{
return sourceMember ?? destMember;
}
}