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

Support deserializing immutable types

      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

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            tkellogg Tim Kellogg
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: