[CSHARP-528] Support deserializing immutable types Created: 16/Jul/12  Updated: 12/Feb/13  Resolved: 12/Feb/13

Status: Closed
Project: C# Driver
Component/s: Feature Request
Affects Version/s: 1.5
Fix Version/s: 1.8

Type: New Feature Priority: Major - P3
Reporter: Tim Kellogg Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: driver, immutable
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to CSHARP-476 Support parameterized constructors on... Closed

 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



 Comments   
Comment by Robert Stam [ 12/Feb/13 ]

Implemented by CSHARP-476.

Comment by Tim Kellogg [ 17/Jul/12 ]

yeah that's what I need. Probably a better implementation too.

Thanks!

Comment by Craig Wilson [ 16/Jul/12 ]

Tim,
Checkout the linked CSHARP-476. It seems like it would fix your issue as well; essentially member deserialization could map to a constructor parameter.

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