[CSHARP-716] Add support for serializing type Tuple introduced in .NET Framework 4 Created: 28/Mar/13 Updated: 02/Apr/15 Resolved: 10/Sep/14 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | None |
| Affects Version/s: | 1.8 |
| Fix Version/s: | 2.0 |
| Type: | New Feature | Priority: | Major - P3 |
| Reporter: | Mary Hamlin | Assignee: | Robert Stam |
| Resolution: | Done | Votes: | 1 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Environment: |
Windows 7 64-bit |
||
| Attachments: |
|
||||||||
| Issue Links: |
|
||||||||
| Description |
|
I have a c# Fund class with a property of type Dictionary<Tuple<int,DateTime>, int> called Quartiles. I added a number of Fund objects to a collection in mongodb called "funds". They were added without any errors. I then attempted to read back the fund documents from the collection. I got an error trying to access the first document in the collection. The error message said there was a problem serializing the Quartiles property and that an item with the same key had already been added. I then attempted to inspect a sample Fund document using the mongo shell and it looks like it wasn't created properly. All relevant information is attached. |
| Comments |
| Comment by Githook User [ 10/Sep/14 ] | |
|
Author: {u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}Message: | |
| Comment by Githook User [ 10/Sep/14 ] | |
|
Author: {u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}Message: | |
| Comment by Robert Stam [ 31/Mar/13 ] | |
|
This issue depends on | |
| Comment by Robert Stam [ 30/Mar/13 ] | |
|
One note about the sample TupleSerializer: it stores the items of the tuple in a BSON array. That seemed like the most natural representation of a Tuple in a BSON document, although other representations are possible. For example, a Tuple could also have been represented as an embedded document instead of an array:
Which in some cases might be a better representation (depending on what sort of queries you write and how you might want to index your documents). The sample TupleSerializer could be modified to use this representation instead. | |
| Comment by Robert Stam [ 30/Mar/13 ] | |
|
While the driver itself can't yet support serializing .NET Framework 4 types, there is nothing preventing a custom serializer being registered for them. I'm attaching a sample serializer for Tuple<T1, T2> that you could incorporate into your application to make serialization of Tuple<T1, T2> work with the current version of the driver (see the attached TupleSerializer.cs file). This serializer will work with any types T1 and T2 as long as T1 and T2 are themselves properly serializable. In order for this serializer to be available you must register it as follows:
You must register serializers as early as possible in the lifecycle of your application. At the very least the serializer must be registered before any attempt is made to serialize or deserialize a Tuple. This serializer could be modified slightly to create new serializers for other Tuples of different cardinality. | |
| Comment by Robert Stam [ 30/Mar/13 ] | |
|
Version 1.8 of the C# driver is built against .NET Framework 3.5. While it can be used with applications that target .NET Framework 4, it does not have built-in serializers for types, like Tuple, that were introduced in .NET Framework 4. |