简体   繁体   English

AutoMapper - 如何在使用AutoMapper.Map时忽略源上的空字段 - 有时候

[英]AutoMapper - how to ignore null fields on the source, while using AutoMapper.Map - sometimes

So we have a situation, where we are mapping from let's say ThingDto : 所以我们有一个情况,我们正在映射,比如说ThingDto

public class ThingDto {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Guid? SomeNiceId { get; set; }
}

and of course - the destination station Thing : 当然 - 目的地站Thing

public class Thing {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Guid? SomeNiceId { get; set; }
}

With that as our context, here is the situation I am trying to solve: We have the DTO as part of a public "contract" library that any outside solution can use in order to send us data. 以此为背景,以下是我要解决的问题:我们将DTO作为公共“合同”库的一部分,任何外部解决方案都可以使用它来向我们发送数据。 In most cases, when we want to use AutoMapper to map from the ThingDto to the Thing object, everything is peachy. 在大多数情况下,当我们想要使用AutoMapperThingDto映射到Thing对象时,一切都很好。 By default, the values that are null on the ThingDto will "null-out" anything that is not null on the Thing object. 默认情况下, ThingDto上为null的值将“ ThingDtoThing对象上不为null的任何内容。

However, we have a situation where we need the null values on the source member (the ThingDto ) just not map to the destination Thing object. 但是,我们需要源成员( ThingDto )上的null值不映射到目标Thing对象。 We can do that with a Condition in the setup, but the issue is that we want to only do that sometimes . 我们可以在设置中使用Condition来做到这一点,但问题是我们有时只想这样做。 Is there a run-time setting that we can put when we are calling AutoMapper.Map<ThingDto, Thing>(thingDto, thing); 当我们调用AutoMapper.Map<ThingDto, Thing>(thingDto, thing);时,我们可以设置运行时设置AutoMapper.Map<ThingDto, Thing>(thingDto, thing); ?

This seems like something that would have been an issue for others as well - but no matter what I do I cannot find anything on it. 这似乎也是其他人的问题 - 但无论我做什么,我都找不到任何东西。 There should be some sort of way to tell AutoMapper on the fly that we don't want to map nulls, but I am coming up with nothing. 应该有某种方式告诉AutoMapper我们不想映射空值,但我什么都没有。

Any help would be greatly appreciated. 任何帮助将不胜感激。

Have you considered setting up multiple AutoMapper profiles for the same types, one that contains the Condition, one that doesn't, and then instantiating the configuration valid at that moment? 您是否考虑为相同类型设置多个AutoMapper配置文件,一个包含条件,一个不包含,然后实例化当时有效的配置?

See: Create two Automapper maps between the same two object types 请参阅: 在相同的两个对象类型之间创建两个Automapper映射

It might take a wrapper class to make it more maintainable, but I'd suggest something like this: 它可能需要一个包装类来使其更易于维护,但我建议这样的事情:

public class NormalProfile : Profile
{
    protected override void Configure()
    {
        base.CreateMap<ThingDto, Thing>();
    }
}

public class ProfileWithCondition : Profile
{
    protected override void Configure()
    {
        base.CreateMap<ThingDto, Thing>().ForAllMembers(opts => opts.Condition((src, dest, srcMember) => srcMember != null));
    }
}

And in usage: 在使用中:

// Some code where you want to include NULL mappings would call this
var config = new MapperConfiguration(cfg => cfg.AddProfile<NormalProfile>);
config.CreateMapper.Map<ThingDto, Thing>(thing);

// Code where you don't, would do this
var config = new MapperConfiguration(cfg => cfg.AddProfile<ProfileWithCondition>);
config.CreateMapper.Map<ThingDto, Thing>(thing);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM