Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-3432

Support for System.Collections.Immutable

    • Type: Icon: Improvement Improvement
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.11.6
    • Component/s: Serialization
    • Labels:
      None
    • Hide

      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?

      Show
      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?

      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

       

            Assignee:
            Unassigned Unassigned
            Reporter:
            admin.mongodb@iovent.net Andreas Wenger
            Votes:
            2 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated: