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

MongoDB C# mapping recursive object with bsonextraelements

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:

      I have following classes:

      public class Property
      { 
          public string Description { get; set; }
          public object Value { get; set; }
      
          [BsonExtraElements]
          public IDictionary<string, Property> OtherData { get; set; }
      } 
      
      public class Device
      {
         public string DeviceId { get; set; }
         public IDictionary<string, Property> Properties { get; set; }
      }
      

      What i want to achieve in json and map to those classes, is:

      {  
         "DeviceId":"asd",
         "Properties":{  
            "PropertyA":{  
               "Description":"some info",
               "PropertyAB":{  
                  "Description":"some info",
                  "Value":"etc"
               }
            },
            "PropertyB":{  
               "Description":"some info",
               "Value":"etc"
            }
         }
      }
      

      So, as you can see, i want to map concrete Model "Property" in recursive way with ExtraElements usage. But above code will fail, as BsonExtraElements can be used only with IDictionary<string, object> interface. Also:

      public class Property : IDictionary<string, Property>
      { 
          public string Description { get; set; }
          public object Value { get; set; }
      }
      

      doesn't work. And to be honest, this semantic would be the most desired. (no need to reach extra Property OtherData) But neither both of those semantics i cannot map properly. Is anyone tried something similar before? Any hints how to achieve this?

            Assignee:
            wan.bachtiar@mongodb.com Wan Bachtiar
            Reporter:
            kurakmen@gmail.com Jakub Dropia
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: