我可以在 C# DocumentDb 驱动程序中使用 polymorphism/inheritance
Can I use polymorphism/inheritance in C# DocumentDb driver
我有一个自定义表单对象结构,我成功地使用了 mongodb。
我一直在研究用 DocumentDb 替换 Mongo 的可能性。
我的 Class 结构包含一个基本控件,不同类型的控件继承自该控件。例如文本框控件、下拉控件
在 mongo 中,我使用鉴别器字段来存储实际类型,在 c# DocumentDb 驱动程序中,我看不到相同的功能。
下面是 mongo 如何存储我的 class 结构的示例。
{
"_t" : "TextboxControl",
"LabelText" : "Location of incident",
"IsRequired" : true,
"_id" : "cbe059d9-b6a9-4de2-b63b-14d44b022e37"
}
在 documentdb 中,结构类似于
{
"LabelText": "Location of incident",
"IsRequired": true,
"id": "cbe059d9-b6a9-4de2-b63b-14d44b022e37"
}
如您所见,mongo 版本有一个“_t”属性 说明实际类型,然后在我读取数据以创建正确类型时使用它。在 documentdb 版本中,它只是一个 fieldtype
我想知道您是否真的需要在 DocumentDb 中执行此操作。
您可以这样分配类型:
private TextBoxControl GetControl(string link)
{
return client.CreateDocumentQuery<TextBoxControl>(link, "SELECT TOP 1 * FROM Controls");
}
我的语法可能不对,但 CreateDocumentQuery<T>
部分应该可以满足您的需求,无需存储类型。
经过数周的搜索,我终于找到了答案
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"
}
我认为他们添加了对 jsonserializer 的支持。您可以通过
RequestOptions 添加它,TypeNameHandling.All
var s = new RequestOptions(){ JsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }}
var result = await Client.CreateDocumentAsync(CollectionUri, @event2, s);
我有一个自定义表单对象结构,我成功地使用了 mongodb。
我一直在研究用 DocumentDb 替换 Mongo 的可能性。
我的 Class 结构包含一个基本控件,不同类型的控件继承自该控件。例如文本框控件、下拉控件
在 mongo 中,我使用鉴别器字段来存储实际类型,在 c# DocumentDb 驱动程序中,我看不到相同的功能。
下面是 mongo 如何存储我的 class 结构的示例。
{
"_t" : "TextboxControl",
"LabelText" : "Location of incident",
"IsRequired" : true,
"_id" : "cbe059d9-b6a9-4de2-b63b-14d44b022e37"
}
在 documentdb 中,结构类似于
{
"LabelText": "Location of incident",
"IsRequired": true,
"id": "cbe059d9-b6a9-4de2-b63b-14d44b022e37"
}
如您所见,mongo 版本有一个“_t”属性 说明实际类型,然后在我读取数据以创建正确类型时使用它。在 documentdb 版本中,它只是一个 fieldtype
我想知道您是否真的需要在 DocumentDb 中执行此操作。
您可以这样分配类型:
private TextBoxControl GetControl(string link)
{
return client.CreateDocumentQuery<TextBoxControl>(link, "SELECT TOP 1 * FROM Controls");
}
我的语法可能不对,但 CreateDocumentQuery<T>
部分应该可以满足您的需求,无需存储类型。
经过数周的搜索,我终于找到了答案
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"
}
我认为他们添加了对 jsonserializer 的支持。您可以通过
RequestOptions 添加它,TypeNameHandling.All
var s = new RequestOptions(){ JsonSerializerSettings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.All }}
var result = await Client.CreateDocumentAsync(CollectionUri, @event2, s);