Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use polymorphism/inheritance in C# DocumentDb driver

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

I've been investigating the possibility of replacing Mongo with DocumentDb.

My Class structure consists of a base control that different types of control inherit from. e.g. Textbox control, Dropdown Control

In mongo I use the discriminator field to store the actual type, in the c# DocumentDb driver I cant see find the same feature.

below is a sample 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 a "_t" property stating the actual type, this is then used when I read the data to create the correct type. In the documentdb version it is simply a fieldtype

like image 472
Steve Parry Avatar asked Sep 09 '15 10:09

Steve Parry


People also ask

Is polymorphism allowed in C?

In computer science, polymorphism is a programming language feature that allows values of different data types to be handled using a uniform interface. According to that definition, no, C doesn't natively support polymorphism.

Can I use polymorphism for inheritance?

Polymorphism is an effect of inheritance. It can only happen in classes that extend one another. It allows you to call methods of a class without knowing the exact type of the class. Also, polymorphism does happen at run time.

Is inheritance possible in C language?

No it doesnt. C is not an Object Oriented language. Inheritance is a property of OO languages.

Which language Cannot support polymorphism?

Explanation: Ada is the language which supports the concept of classes but doesn't support the polymorphism feature. It is an object-based programming language.


2 Answers

After many weeks of searching I eventually came across the answer

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

Basically this library extends the DocumentDb's C# SDK and allows for custom JSON settings to be applied. Under the hood the documentdb driver users json.net.

I now get the property "$type" which is a feature built into newtonsoft's excellent json.net library.

My json now looks like

{
  "$type" : "MyNameSpace.DropDownSingleFormBuilderControlTemplate, MyLibrary",
  "LabelText" : "Label Text"
  "IsRequired" : true,
  "_id" : "cbe059d9-b6a9-4de2-b63b-14d44b022e37"
}
like image 122
Steve Parry Avatar answered Oct 03 '22 10:10

Steve Parry


I think they added support for jsonserializer. You can add it via the
RequestOptions, TypeNameHandling.All

var s =  new RequestOptions(){ JsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }}
var result = await Client.CreateDocumentAsync(CollectionUri, @event2, s);
like image 26
hannes neukermans Avatar answered Oct 03 '22 10:10

hannes neukermans