使用 Mapster 映射时获取空集合而不是 null

Get Empty Collection instead of null when mapping with Mapster

我们有一个涉及多个系统的庞大数据驱动应用程序,因此需要大量映射。 由于性能问题,我们将从 AutoMapper 迁移到 Mapster。

到目前为止,使用 Mapster 一切都很好,但是在映射 Collections Mapster returns null 值而不是空 Collection.

Automapper 过去默认 return 清空集合,但我不知道如何在 Mapster 中做到这一点。

我已尝试执行以下操作,但它不起作用

TypeAdapterConfig.GlobalSettings.ForDestinationType<ICollection>().IgnoreNullValues(true);

TypeAdapterConfig.GlobalSettings.ForType(typeof(ICollection), typeof(ObservableCollection<>))
                        .IgnoreNullValues(true);

TypeAdapterConfig.GlobalSettings.ForType(typeof(ObservableCollection<>), typeof(ICollection))
                        .IgnoreNullValues(true);

任何帮助都会很棒

多亏了 Mapster 团队,我才让它工作。将它张贴在这里以防其他人需要它

您可以通过以下方式显式配置它:

TypeAdapterConfig.GlobalSettings.Default
    .AddDestinationTransform((IReadOnlyList<ChildDto> list) => list ?? new List<ChildDto>());

Mapster 还添加了对 AddDestinationTransform

的通用支持
config.Default.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);

这对我有用。