我可以在C#DocumentDb驱动程序中使用多态/继承吗?

Ste*_*rry 6 .net c# polymorphism inheritance azure-cosmosdb

我有一个自定义表单对象结构,我成功使用mongodb.

我一直在研究用DocumentDb替换Mongo的可能性.

我的类结构由不同类型的控件继承的基本控件组成.例如文本框控件,下拉控件

在mongo我使用鉴别器字段来存储实际类型,在c#DocumentDb驱动程序中我看不到找到相同的功能.

下面是mongo如何存储我的类结构的示例.

{
  "_t" : "TextboxControl",
  "LabelText" : "Location of incident",
  "IsRequired" : true,
  "_id" : "cbe059d9-b6a9-4de2-b63b-14d44b022e37"
}
Run Code Online (Sandbox Code Playgroud)

在documentdb中,结构看起来像

{
  "LabelText": "Location of incident",
  "IsRequired": true,
  "id": "cbe059d9-b6a9-4de2-b63b-14d44b022e37"
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,mongo版本具有一个"_t"属性来说明实际类型,然后在我读取数据时使用它来创建正确的类型.在documentdb版本中,它只是一个字段类型

Ste*_*rry 3

经过几周的搜索,我终于找到了答案

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

基本上,该库扩展了 DocumentDb 的 C# SDK,并允许应用自定义 JSON 设置。在幕后,documentdb 驱动程序用户使用 json.net。

我现在获得了属性“$type”,这是 newtonsoft 优秀的 json.net 库中内置的一个功能。

我的 json 现在看起来像

{
  "$type" : "MyNameSpace.DropDownSingleFormBuilderControlTemplate, MyLibrary",
  "LabelText" : "Label Text"
  "IsRequired" : true,
  "_id" : "cbe059d9-b6a9-4de2-b63b-14d44b022e37"
}
Run Code Online (Sandbox Code Playgroud)