[CSHARP-939] Revisit Dynamic DictionaryRepresentation Created: 21/Mar/14  Updated: 02/Apr/15  Resolved: 01/May/14

Status: Closed
Project: C# Driver
Component/s: Serialization
Affects Version/s: 1.9
Fix Version/s: 2.0

Type: Improvement Priority: Minor - P4
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Major Change

 Description   

We currently support the following dictionary representations:

    public enum DictionaryRepresentation
    {
        Dynamic,
        Document,
        ArrayOfArrays,
        ArrayOfDocuments
    }

The last 3 representations correspond to:

{ dictionary : { key1 : value1, ... } }
{ dictionary : [[key1, value1], ...] }
{ dictionary : [{ key1 : value1 }, ...] }

Each representation has its advantages. The first is probably the most natural representation and what most users would instinctively expect a Dictionary to be represented as. However, it can only be used when all they key values are strings and when the values of the strings are valid element names for a BSON document.

The current default representation is Dynamic, which means that the serializer examines the keys and uses the Document representation if possible, and falls back to the ArrayOfArrays representation if not.

Dynamic has pluses and minuses. On the plus side, serialization always succeeds and your Dictionary will be successfully saved to the database no matter what values the keys have. On the minus side, you will not have a consistent representation of your Dictionary values in your collection, because depending on the values of the keys one or the other representation will have been chosen. This is not a problem for round-tripping your Dictionary, but can be a huge problem if you need to write queries against your Dictionary values.

The task in this ticket is to reconsider whether we should just remove the Dynamic representation or perhaps at least make it not be the default.

It is tempting to make Document the default representation, but the problem with that is that if any of your keys have values that are not valid element names serialization will throw an exception. Is that acceptable?



 Comments   
Comment by Githook User [ 01/May/14 ]

Author:

{u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-939: Get rid of the Dynamic DictionaryRepresentation.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/b134ec6a24f7c99992a651637734918476c54915

Comment by Robert Stam [ 29/Apr/14 ]

We have made the decision to eliminate the Dynamic dictionary representation entirely, and default to Document.

The good thing about this decision is that you can now count on a consistent representation in your database for Dictionary values, which means that you can more easily write queries against Dictionary values.

The possible downside is that if you were serializing any Dictionaries where some of the key values were not valid BSON element names, you will now get runtime exceptions (instead of dynamically switching to ArrayOfArrays representation for just those cases).

If your Dictionary has key values that are not valid element names, you should configure your serialization to use one of the other representations: ArrayOfArrays or ArrayOfDocuments.

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