【发布时间】:2017-02-09 15:41:12
【问题描述】:
我正在使用 Mapster 将 Dto 实例映射到 Model 对象。
Dtos 由 Javascript 客户端发送,仅发送更新的属性。
我想忽略 null 值,并让 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 按名称自动映射所有字符串属性,但保留忽略空值的“类似补丁”的逻辑。
【问题讨论】:
-
您没有回答自己的问题吗?在 Adapt() 方法中,忽略 Dto 中的空值。
-
我想使用 Automapping,并获得这种逻辑。我怎么能这样做?
-
检测到 GOT 风扇