我目前正在开发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; }
}
现在的问题是,复合结构没有被正确存储。试剂在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'这里吗? '列表'不够吗?你可能会觉得它更开心吗?请注意,我也怀疑'IReagant'是否是一个基本问题,除非它存储了类型信息(即具体的反对类型),它不知道要重建什么 –
当我在运行时调试时检查“crafts”时,它包含一个复合结构。我正在使用MS的Parallel .NET 4.0 lib来计算对象图,因此需要使用ConcurrentBag来避免使用锁。我已经怀疑过这样的事情了。它不会支持接口,但Craft也是一个实现的IReagent并被存储。 –
存储'Craft'的实例不同于存储'IReagant'的实例,该实例恰好是* Craft'。特别是对于序列化库(相信我; p) –