BSON Serializing and Deserializing an Enumeration Type's ComponentModel.DescriptionAttribute into a MongoDB collection

248 views
Skip to first unread message

Anthony Habash

unread,
Feb 5, 2016, 2:52:22 PM2/5/16
to mongodb-user

Greetings thank you for your time,

Scenario: Currently when submitting an object model with enumerated types for serialization into a MongoDB collection the enumerated types are stored by their numeric values. It is possible to store them by their string values through the application of a Convention or individually using BsonRepresentation. However each one of my enumerated types also consists of an instance of CompomentModel.DescriptionAttribute.

Question/Desired Capability: I would like to be able to use the DescriptionAttribute when storing the values of Enumerated Types.

The reason is readability - some of my enumerated types are quite verbose and the Description is the most readable form of representation while the numeric values require an extra level of translation. I could store the Descriptions within my Object Models as a separate property from the enumerated values, but that feels like a workaround.

Is there a means of implementing the behavior using the MongoDB Driver where EnumeratedType's DescriptionAttribute gets serialized and deserialized instead of the numeric or string value?

Cheers,

Robert Stam

unread,
Feb 5, 2016, 7:05:59 PM2/5/16
to mongod...@googlegroups.com
The following should work:

1) Write a custom serializer for your enums that serializes/deserializes the enums the way you want

2) Write a custom serialization provider that chooses your serializer when the type is one of your enums

3) Register your custom serialization provider

Instead of steps 2 and 3 you could just register an instance of your custom serializer for each of your enums. If you only have a small number of enums that would be easier.

--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at https://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/d84411b0-3e97-4d6c-923e-fd4742720a98%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages