首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mapster -如何忽略null属性的映射

Mapster -如何忽略null属性的映射
EN

Stack Overflow用户
提问于 2017-02-09 23:41:12
回答 1查看 5.2K关注 0票数 1

我使用Mapster将Dto实例映射到Model对象。

Dtos由Javascript客户端发送,仅发送更新的属性。

我想忽略空值,并让Mapster保持此属性的模型实例不变。

为了更好地解释场景,我们提供了一个简化的示例:

代码语言:javascript
复制
// 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按名称自动映射所有字符串属性,但保留忽略空值的“补丁式”逻辑。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-14 12:13:52

您可以使用IgnoreNullValues

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42140898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档