我目前正在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" : []
}
]
}
]
}
]
}
可能是什么问题呢?
最佳答案 问题是你正在使用抽象列表,它不能将这些序列化为JSON,所以基本上你需要编写自己的自定义序列化.这是我写的自定义序列化的示例:
public class FieldsWrapper : IBsonSerializable
{
public List<DataFieldValue> DataFieldValues { get; set; }
public object Deserialize(MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
{
if (nominalType != typeof(FieldsWrapper)) throw new ArgumentException("Cannot deserialize anything but self");
var doc = BsonDocument.ReadFrom(bsonReader);
var list = new List<DataFieldValue>();
foreach (var name in doc.Names)
{
var val = doc[name];
if (val.IsString)
list.Add(new DataFieldValue {LocalIdentifier = name, Values = new List<string> {val.AsString}});
else if (val.IsBsonArray)
{
DataFieldValue df = new DataFieldValue {LocalIdentifier = name};
foreach (var elem in val.AsBsonArray)
{
df.Values.Add(elem.AsString);
}
list.Add(df);
}
}
return new FieldsWrapper {DataFieldValues = list};
}
public void Serialize(MongoDB.Bson.IO.BsonWriter bsonWriter, Type nominalType, IBsonSerializationOptions options)
{
if (nominalType != typeof (FieldsWrapper))
throw new ArgumentException("Cannot serialize anything but self");
bsonWriter.WriteStartDocument();
foreach (var dataFieldValue in DataFieldValues)
{
bsonWriter.WriteName(dataFieldValue.LocalIdentifier);
if (dataFieldValue.Values.Count != 1)
{
var list = new string[dataFieldValue.Values.Count];
for (int i = 0; i < dataFieldValue.Values.Count; i++)
list[i] = dataFieldValue.Values[i];
BsonSerializer.Serialize(bsonWriter, list);
}
else
{
BsonSerializer.Serialize(bsonWriter, dataFieldValue.Values[0]);
}
}
bsonWriter.WriteEndDocument();
}
}
在您的情况下,我会在Reagent类的级别上编写自定义序列化