如果不为空,则可以自动映射器忽略目标,仅更改空字段 [英] Can automapper ignore destination if it is not null / only change null fields

查看:100
本文介绍了如果不为空,则可以自动映射器忽略目标,仅更改空字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我正在开发一个Web服务,我想允许具有空字段的输入表示不进行更新".输入对象非常相似,但与数据库模型并不相同,因此我们使用自动映射器进行转换.

Background: I'm working on a webservice that I want to allow input that has a null field to mean, "don't do an update". The input object is very similar but not identical to the database model, so we're using automapper to do the transforms.

因此,在进行更新的情况下,我希望能够获取现有值,使用它们覆盖输入中的任何空字段,然后将其保存以进行整个更新. 那么有没有一种方法可以使automapper仅在目标字段为null时才将值放入目标?

So in the case of an update, I'd like to be able to take the existing values, use them to override any of the null fields in the input, and then save that to do the whole update. So is there a way to make automapper only put values into the destination if the destination field is null?

推荐答案

是的,可以,但是您可能不想经历麻烦.为此,您需要为要执行此操作的对象上的每个字段都具有一个自定义地图处理程序(您可以在相同类型的属性之间共享该自定义处理程序,但我不是100%肯定不看我的一些旧代码.

Yes, it can, but you probably wouldn't want to go through the hassle. To do this, you're going to need to have a custom map handler for every field on the object that you want to do this (You may be able to share the custom handler among properties of the same type, but I'm not 100% sure without looking at some of my old code).

这篇关于如果不为空,则可以自动映射器忽略目标,仅更改空字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆