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

标签 c# .net mapster

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

到目前为止,Mapster 的一切都很好,但是当映射 Collections 时,Mapster 返回 null 值而不是空的 Collection

Automapper 过去常常默认返回空集合,但我不知道如何在 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);

这对我有用。

关于c# - 使用 Mapster 映射时获取空集合而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60669259/

相关文章:

C# Linq 列名作为两个词(去年)或作为带前导零的数字 (01)

c# - WCF - 在 CMS 中使用 Web 控制的客户端安全

c# - 如何在调用 SmtpClient.Send() 之前知道域是错误的?

c# - 提高单个网络服务的超时时间

c# - Mapster Adpat 不工作

C#:BackgroundWorker 克隆资源?

c# - 防止Win32绘制经典标题栏

c# - 如何避免无限循环,最佳实践

c# - Mapster - 如何忽略空属性的映射

c# - Mapster.Tool 使用 codegen 生成映射器而不是 DTO