Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-854

Implementing custom simple mapping for object types



    • Backwards Compatibility:
      Fully Compatible


      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

      { return toTimespanString(); }


      { 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?




            curt Curt Mayers
            0 Vote for this issue
            1 Start watching this issue