I am currently developing a document store in Mongo DB which contains a complete material breakdown of a specific item. The breakdown is calculated and contains a composite structure.
The domain model:
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; }
}
Now the problem is that a composite structure is not correctly stored. Reagents stays null in mongodb.
/* 28 */
{
"_id" : ObjectId("4e497efa97e8b617f0d229d4"),
"ItemId" : 52186,
"Quantity" : 0,
"SpellId" : 0,
"Skill" : 475,
"Profession" : 8,
"Reagents" : { }
}
Example of how it should look
{
"_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" : []
}
]
}
]
}
]
}
What could be the problem?
ConcurrentBag<T>here? wouldList<T>not suffice? you might find it happier w/ that? Note that I also wonder ifIReagantbeing an interface is the fundamental problem, as unless it stores type info (i.e. the concrete reagant type) it can't know what to reconstructCraftis different to storing an instance ofIReagantthat happens to be aCraft. Especially to serialization libraries (trust me on this ;p)Craftit knows the type - it isCraft. Now: what object do you create for anIReagant?