Can I use polymorphism / inheritance in the C # DocumentDb driver

I have a custom form object structure that I successfully use with mongodb.

I am exploring the possibility of replacing Mongo with DocumentDb.

The My Class structure consists of a basic control from which various types of control are inherited. e.g. Text Field Management, Dropdown Control

In mongo, I use the discriminator field to store the actual type in the C # DocumentDb driver, which I cannot see to find the same function.

Below is an example of how mongo stores my class structure.

{ "_t" : "TextboxControl", "LabelText" : "Location of incident", "IsRequired" : true, "_id" : "cbe059d9-b6a9-4de2-b63b-14d44b022e37" } 

In documentdb, the structure looks like

 { "LabelText": "Location of incident", "IsRequired": true, "id": "cbe059d9-b6a9-4de2-b63b-14d44b022e37" } 

As you can see, the mongo version has the property “_t” indicating the actual type, this is used when I read the data to create the correct type. In documentdb version, it's just a field type

+5
source share
2 answers

After many weeks of searching, I eventually came across an answer

https://github.com/markrexwinkel/azure-docdb-linq-extension

Basically, this library extends DocumentDb C # SDK and allows you to apply custom JSON settings. Under the hood of the user documentdb users json.net.

Now I get the "$ type" property, which is a function built into the excellent json.net library in newtonsoft.

Now my json looks like

 { "$type" : "MyNameSpace.DropDownSingleFormBuilderControlTemplate, MyLibrary", "LabelText" : "Label Text" "IsRequired" : true, "_id" : "cbe059d9-b6a9-4de2-b63b-14d44b022e37" } 
+3
source

I wonder if you need to do this in DocumentDb at all.

You can assign a type as such:

 private TextBoxControl GetControl(string link) { return client.CreateDocumentQuery<TextBoxControl>(link, "SELECT TOP 1 * FROM Controls"); } 

My syntax may be missing, but the CreateDocumentQuery<T> should do what you need without requiring you to save this type.

0
source

All Articles