gpt4 book ai didi

c# - Mapster - 如何忽略空属性的映射

转载 作者:太空宇宙 更新时间:2023-11-03 23:03:35 29 4
gpt4 key购买 nike

我正在使用 Mapster 将 Dto 实例映射到模型对象。

Dto 由 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;
}

编辑:关键是我不想为我的 Dto 中的数千个属性中的每一个写下映射条件。我希望 Mapster 按名称自动映射所有字符串属性,但保留忽略空值的“类似补丁”的逻辑。

最佳答案

您可以使用 IgnoreNullValues .

关于c# - Mapster - 如何忽略空属性的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42140898/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com