[CSHARP-854] Implementing custom simple mapping for object types Created: 29/Oct/13  Updated: 05/Apr/19  Resolved: 20/Mar/14

Status: Closed
Project: C# Driver
Component/s: Documentation, Feature Request
Affects Version/s: 1.8.3
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: Curt Mayers Assignee: Unassigned
Resolution: Done Votes: 0
Labels: BSON,, Type, mapping, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All


Backwards Compatibility: Fully Compatible

 Description   

I would like to provide simplified BSON mappings for a number of business object types (I'll provide some concrete examples further down).

I will be storing millions of record of very complex business objects, for which many custom sub-objects have been designed. Many of them can render as a simple string (and load as well), but because "String" is a sealed type in C#, I cannot build them as descendants of a string.

For compactness, clarity, and ease of querying, I would like them to render into BSON as a simple string. I have searched the documentation, newsgroups, and other resources, for examples of custom class mappings, but have not found sufficiently appropriate documentation or examples for me to build such a mapping.

It might help to provide some simple examples:

An timespan type:

class TimeSpan
{
private int _seconds;
private string _toTimespanString()

{ ... implementation }

private void fromTimespanString(string s)

{ implementation... }

public string TimeString
{
get

{ return toTimespanString(); }

set

{ toTimespanString(value); }

}
}

In the above example, I would like to serialize the above type as a string (e.g. "5h23m"), and deserialize it from same. The BSON serializer can do it, but it serializes as a Document, meaning that the value is buried with it.

I have a few classes that have a similar construct: a business object is built around what is essentially a string value, in order to do conversion, calculation, validation, etc. But when rendered, I'd like them to appear in BSON documents as simple strings (examples include URIs, controlled vocabulary strings, and the TimeSpan indicated above).

These objects may have many properties that are used for manipulation within a program, but only a single [BSonElement] string property that is used for setting and retrieving values.

This might make a good generalized enhancement to the driver (e.g. allow a BSonStringRepresentation property as a convention), but I would like to know of any specific references / examples out there for doing such a custom BSON type mapping.

Can you help?



 Comments   
Comment by Craig Wilson [ 29/Oct/13 ]

Thanks for your question. The documentation for serialization exists here: http://docs.mongodb.org/ecosystem/tutorial/serialize-documents-with-the-csharp-driver/. It's long and involved. The specific section you care about can be gotten to at this link: http://docs.mongodb.org/ecosystem/tutorial/serialize-documents-with-the-csharp-driver/#write-a-custom-serializer.

Essentially, what you need to do is write an implementation of an IBsonSerializer. Effectively, you'll take your TimeSpan class and convert it into a string on serialization and then parse that string on deserialization and convert it back to a TimeSpan. The best examples for how to do this would be to look in the source code at our existing implementations of serializers here: https://github.com/mongodb/mongo-csharp-driver/tree/master/MongoDB.Bson/Serialization/Serializers.

Craig

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