Details
-
Improvement
-
Resolution: Unresolved
-
Major - P3
-
None
-
2.11.6
-
None
Description
The MongoDB C# Driver does not support System.Collections.Immutable out of the box yet. For example, when a ImmutableList<T> is deserialized, the following exception is thrown:
An error occurred while deserializing the XY property of class XY: Type 'System.Collections.Immutable.ImmutableList`1[[...]]' does not have a suitable constructor or Add method.' |
It is possible, yet cumbersome, to add custom serializers for supporting them.
For example, for ImmutableList<T>:
public class ImmutableListSerializer<TValue> : EnumerableInterfaceImplementerSerializerBase<ImmutableList<TValue>, TValue> { |
|
|
protected override object CreateAccumulator() => |
ImmutableList.CreateBuilder<TValue>();
|
|
|
protected override ImmutableList<TValue> FinalizeResult(object accumulator) => |
((ImmutableList<TValue>.Builder)accumulator).ToImmutable();
|
|
|
}
|
Since immutable collections are a native part of the .NET Runtime and will get increasing attention with the new immutable `record`s possible in C# 9, it would be very useful to support them out-of-the-box.
More example code, yet outdated, can be found in this github project: https://github.com/marcpiechura/MongoDB.Immutable