将AutoMapper与Google Protocol Buffers一起使用3

问题描述 投票:1回答:1

我想将AutoMapper与proto3一起使用,但我遇到的最大问题是从源属性映射,这可能允许null成为一个从未做过的原型。手动执行此类填充时,必须执行以下操作:

var proto = new Proto();

if (source.Field != null)
{
    proto.Field = source.Field;
}

我仍然觉得这很荒谬,但这显然与proto3有什么关系。

无论如何,这意味着映射必须具有条件,以确保null值不会传播到proto:

config
    .CreateMap<Source, Proto>()
    .ForMember(
        x => x.Field,
        options => options.Condition(source => source.Field != null));

因为我的原型中有很多属性,所以我能感觉到它变老了。

我想知道的是,我是否有办法在更高的抽象层次上处理这个问题?

c# automapper protocol-buffers proto3
1个回答
0
投票

您可以在ForAllOtherMembers输出上使用CreateMap<Source,Proto>方法并指定条件。这将解决您没有为每个属性指定的问题

示例代码

config
    .CreateMap<Source, Proto>()
    .ForAllOtherMembers(
        options => options.Condition((src, dest, srcValue) => srcValue != null));
© www.soinside.com 2019 - 2024. All rights reserved.