[CSHARP-3432] Support for System.Collections.Immutable Created: 19/Feb/21  Updated: 08/Jan/24

Status: Backlog
Project: C# Driver
Component/s: Serialization
Affects Version/s: 2.11.6
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Andreas Wenger Assignee: Unassigned
Resolution: Unresolved Votes: 2
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: Implement 3.0 release
Quarter: FY24Q4
Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 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

 



 Comments   
Comment by James Kovacs [ 19/Feb/21 ]

Hi, Andreas,

We tested Record types recently with the MongoDB .NET/C# driver and they were already supported by our infrastructure for immutable types. However we did not test immutable collections. We will consider supporting them in a future release. Thank you for your suggestion.

Sincerely,
James

Generated at Wed Feb 07 21:45:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.