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

Support deserializing immutable types

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: 1.5
    • Fix Version/s: 1.8
    • Component/s: Feature Request
    • Labels:

      Description

      I'm trying to write a F# driver implementation that wraps what the C# driver does. Right now the main thing that I'm getting stuck on is deserializing immutable record types. If I have this F# record:

      type Person =

      { Id : BsonObjectId Name : string }

      it compiles to [roughly] this C# code

      public class Person
      {
      private readonly BsonObjectId id;
      private readonly string name;

      public Person(BsonObjectId id, string name)

      { this.id = id; this.name = name; }

      public BsonObjectId Id { get

      { return id; }

      }
      public string Name { get

      { return name; }

      }
      }

      I can configure the BsonClassMapSerializer to serialize objects just fine, however when I try deserializing it skips all the properties of the record because they don't have setter methods.

      A big part of the problem is that BsonClassMapSerializer does 90% of what I need, but it's an internal class. Even if it were public, it's too much of a behemoth to be able to customize via subclassing. A better solution might be to add configuration to class maps. Perhaps an interface such as:

      interface IMemberAccumulator

      { object Initialize(Type nominalType); void AccumulateMember(object state, Type nominalType, MemberInfo memberInfo, object value); object GetFinalValue(object state, Type nominalType); }

      and a `BsonClassMap.SetMemberAccumulator(IMemberAccumulator)` would probably get me what I need. I'm sure this sort of thing might even be useful for other languages, including C#.

      https://github.com/tkellogg/MongoDB.FSharp

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: