Force any class to be serialized as a BSON class map and dictionary implementations to serialize keys as regular BSON elements #221
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Force any class to be serialized as BSON class map
Because some requirements in my project, I had to create a base class derived by some domain objects which implement
IDictionary<TKey, TValue>
, but these domain objects are still POCOs and I want them to be serialized using regular BSON class map serializer.When I ran some test I found some issue: my domain object wasn't getting an auto-assigned GUID. After clonning the driver source code, I debugged the code step by step and I found that my domain objects were serialized as collections rather than POCOs, because they inherit a base class which implements
IDictionary<TKey, TValue>
as I mentioned above.Then, I thought, what about a way to configure that certain types must be serialized as class maps to solve my issue?, and I implemented a serialization provider
ForceAsBsonClassMapSerializationProvider
to fix my problem.For example, this new provider may be used as follows:
It can be also registered a concrete type to be forced as a BSON class map:
Or also both:
BSON class maps of classes implementing
IDictionary<string, object>
It might happen that a class representing a domain object or an entity may also implement
IDictionary<TKey, TValue>
interface. In my case, it has happened to let my domain objects be extensible but also contain regular properties.For now, Mongo C# driver supports extra elements as a dictionary property, but what about the above feature of holding those extra elements as keys of an entity implementing
IDictionary<string, object>
?In order to get this new feature working, I've modified
BsonClassMapSerializer
to detect if a document type is alsoIDictionary<string, object>
itself, and serialize its key-value pairs as regular BSON elements. The so-called feature applies to both serialization and deserialization.Final thoughts
Because I feel that other developers might encounter the same limitations and they might need to workaround them, what about including these features as part of the driver as is?
Thank you in advance and I'll look forward for your comments, suggestions...!