如何在反序列化的 MongoDB 文档中获取对父对象的引用?
How to get a reference to the parent object in a deserialized MongoDB document?
希望有人能帮忙。我开始了解 MongoDB 的 C# 驱动程序及其处理序列化的方式。考虑以下示例 类:
class Thing
{
[BsonId]
public Guid Thing_ID { get; set; }
public string ThingName {get; set; }
public SubThing ST { get; set; }
public Thing()
{
Thing_ID = Guid.NewGuid();
}
}
class SubThing
{
[BsonId]
public Guid SubThing_ID { get; set; }
public string SubThingName { get; set; }
[BsonIgnore]
public Thing ParentThing { get; set; }
public SubThing()
{
SubThing_ID = Guid.NewGuid();
}
}
请注意,SubThing 有一个 属性 引用其父项。所以在创建对象时我这样做:
Thing T = new Thing();
T.ThingName = "My thing";
SubThing ST = new SubThing();
ST.SubThingName = "My Subthing";
T.ST = ST;
ST.ParentThing = T;
ParentThing 属性 设置为 BsonIgnore,否则在序列化为 MongoDB.
时会导致循环引用
当我序列化为 MongoDB 时,它创建的文档完全符合我的预期:
{
"_id" : LUUID("9d78bc5c-2abd-cb47-9478-012f9234e083"),
"ThingName" : "My thing",
"ST" : {
"_id" : LUUID("656f17ce-c066-854d-82fd-0b7249c80ef0"),
"SubThingName" : "My Subthing"
}
问题出在这里:当我反序列化时,我丢失了 SubThing 对其父对象的引用。有什么方法可以配置反序列化,使 ParentThing 属性 始终是其父文档?
来自 mongodb 网站
Implementing ISupportInitialize -
The driver respects an entity implementing ISupportInitialize which contains 2 methods, BeginInit and EndInit. These method are called before deserialization begins and after it is complete. It is useful for running operations before or after deserialization such as handling schema changes are pre-calculating some expensive operations.
所以,Thing
将实现 ISupportInitialize
,函数 BeginInit
将保持为空,Endinit
将包含 St.ParentThing = this;
在这个抽象层次上,很难给出明确的答案。
一种方法是让 class implement ISupportInitialize 在反序列化后提供一个钩子。这可能是解决手头问题的最简单方法。否则,相同的 link 还展示了如何编写自定义序列化程序,但在这种情况下不需要这样做。
希望有人能帮忙。我开始了解 MongoDB 的 C# 驱动程序及其处理序列化的方式。考虑以下示例 类:
class Thing
{
[BsonId]
public Guid Thing_ID { get; set; }
public string ThingName {get; set; }
public SubThing ST { get; set; }
public Thing()
{
Thing_ID = Guid.NewGuid();
}
}
class SubThing
{
[BsonId]
public Guid SubThing_ID { get; set; }
public string SubThingName { get; set; }
[BsonIgnore]
public Thing ParentThing { get; set; }
public SubThing()
{
SubThing_ID = Guid.NewGuid();
}
}
请注意,SubThing 有一个 属性 引用其父项。所以在创建对象时我这样做:
Thing T = new Thing();
T.ThingName = "My thing";
SubThing ST = new SubThing();
ST.SubThingName = "My Subthing";
T.ST = ST;
ST.ParentThing = T;
ParentThing 属性 设置为 BsonIgnore,否则在序列化为 MongoDB.
时会导致循环引用当我序列化为 MongoDB 时,它创建的文档完全符合我的预期:
{
"_id" : LUUID("9d78bc5c-2abd-cb47-9478-012f9234e083"),
"ThingName" : "My thing",
"ST" : {
"_id" : LUUID("656f17ce-c066-854d-82fd-0b7249c80ef0"),
"SubThingName" : "My Subthing"
}
问题出在这里:当我反序列化时,我丢失了 SubThing 对其父对象的引用。有什么方法可以配置反序列化,使 ParentThing 属性 始终是其父文档?
来自 mongodb 网站
Implementing ISupportInitialize - The driver respects an entity implementing ISupportInitialize which contains 2 methods, BeginInit and EndInit. These method are called before deserialization begins and after it is complete. It is useful for running operations before or after deserialization such as handling schema changes are pre-calculating some expensive operations.
所以,Thing
将实现 ISupportInitialize
,函数 BeginInit
将保持为空,Endinit
将包含 St.ParentThing = this;
在这个抽象层次上,很难给出明确的答案。
一种方法是让 class implement ISupportInitialize 在反序列化后提供一个钩子。这可能是解决手头问题的最简单方法。否则,相同的 link 还展示了如何编写自定义序列化程序,但在这种情况下不需要这样做。