我正在使用Mapster将Dto实例映射到Model对象。
Dtos由Javascript客户端发送,仅发送更新的属性。
我想忽略空值,并让Mapster为此属性保留模型实例。
更好地解释该场景的简化示例:
// My .Net Dto class, used for client/server communication.
public class PersonDto
{
public string Id { get; set; }
public string Name { get; set; }
public string Family { get; set; }
}
// My Model class. Let's assume is the same data as per the Dto.
public class Person
{
public string Id { get; set; }
public string Name { get; set; }
public string Family { get; set; }
}
public void Update()
{
var existingPerson = new Person
{
Id = "A",
Name = "Ned",
Family = "Stark"
};
var patchDataSentFromClient = new PersonDto
{
Id = "A",
Name = "Rob"
};
patchDataSentFromClient.Adapt(existingPerson);
// Here existingPerson.Family should be "Stark", but it gets emptied out.
// the mapping should be equivalent to:
// if (patchDataSentFromClient.Family != null) existingPerson.Family = patchDataSentFromClient.Family;
}
编辑:重点是我不想写下我的Dtos中数以千计的每个属性的映射条件。我希望Mapster按名称自动执行所有字符串属性,但保持忽略空值的“类补丁”逻辑。