【发布时间】:2011-08-18 09:34:00
【问题描述】:
我目前正在 Mongo DB 中开发一个文档存储,其中包含特定项目的完整材料分解。分解计算并包含复合结构。
领域模型:
public interface IReagent
{
int ItemId { get; set; }
int Quantity { get; set; }
ConcurrentBag<IReagent> Reagents { get; set; }
}
public class Craft : IReagent
{
public int ItemId { get; set; }
public int Quantity { get; set; }
public int SpellId { get; set; }
public int Skill { get; set; }
public Profession Profession { get; set; }
public ConcurrentBag<IReagent> Reagents { get; set; }
}
public class Reagent : IReagent
{
public int ItemId { get; set; }
public int Quantity { get; set; }
public ConcurrentBag<IReagent> Reagents { get; set; }
}
现在的问题是没有正确存储复合结构。 Reagents 在 mongodb 中保持为空。
/* 28 */
{
"_id" : ObjectId("4e497efa97e8b617f0d229d4"),
"ItemId" : 52186,
"Quantity" : 0,
"SpellId" : 0,
"Skill" : 475,
"Profession" : 8,
"Reagents" : { }
}
外观示例
{
"_id" : ObjectId("4e497efa97e8b617f0d229d4"),
"ItemId" : 52186,
"Quantity" : 0,
"SpellId" : 0,
"Skill" : 475,
"Profession" : 8,
"Reagents" : [
{
"ItemId" : 521833,
"Quantity" : 3,
"SpellId" : 0,
"Skill" : 400,
"Profession" : 7,
"Reagents" : [
{
"ItemId" : 52186,
"Quantity" : 3,
"SpellId" : 0,
"Skill" : 475,
"Profession" : 8,
"Reagents" : [
{
"ItemId" : 52183,
"Quantity" : 2,
"Reagents" : []
},
{
"ItemId" : 521832,
"Quantity" : 1,
"Reagents" : []
}
]
},
{
"ItemId" : 52386,
"Quantity" : 2
"SpellId" : 0,
"Skill" : 400,
"Profession" : 8,
"Reagents" : [
{
"ItemId" : 52383,
"Quantity" : 2,
"Reagents" : []
},
{
"ItemId" : 523832,
"Quantity" : 1,
"Reagents" : []
}
]
}
]
}
]
}
可能是什么问题?
【问题讨论】:
-
我认为传入时它是非空的?我可以检查一下 - 你真的需要
ConcurrentBag<T>吗?List<T>不够吗?你会发现它更快乐吗?请注意,我还想知道IReagant作为接口是否是根本问题,因为除非它存储类型信息(即具体的 reagant 类型),否则它不知道要重建什么 -
当我在运行时调试中检查“crafts”时,它包含一个复合结构。我正在使用 MS 的 Parallel .NET 4.0 库来计算对象图,因此需要 ConcurrentBag 来避免使用锁。我已经在怀疑这样的事情了。它不支持接口,尽管 Craft 也是一个实现 IReagent 并被存储。
-
存储
Craft的实例与存储IReagant的实例不同,后者恰好是Craft。尤其是序列化库(相信我;p) -
我相信你 :D 只是想知道为什么序列化程序识别 Craft 而不是 IEnumerable
。它基本上是一个混合了 Craft 或 Reagent 的具体实例的列表。我猜 BSON 序列化器并不那么聪明。我还用 Redis(和 redis 驱动程序,在这种情况下它只是工作)做了一些测试。我想添加一个自定义序列化程序(如下所示)可以解决问题:) -
因为在
Craft的情况下它知道类型 - 它是Craft。现在:您为IReagant创建什么对象?
标签: c# bson mongodb-.net-driver