我们有一个涉及多个系统的庞大数据驱动应用程序,因此需要大量映射。 由于性能问题,我们将从 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/