存储复合/嵌套的对象图

编程入门 行业动态 更新时间:2024-10-19 03:23:15
本文介绍了存储复合/嵌套的对象图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在开发中蒙戈DB文件存储其中包含的具体项目的完整材料破裂。该故障计算,并包含一个复合结构

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.

域模型:

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中

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" : { } }

的例子它应该如何看

{ "_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,所以基本上你需要编写自己的自定义序列化。这是自定义序列化的例子中,我写道:

The problem is you are using list of abstractions and it cannot serialize these to JSON, so basically you would need to write your own custom serialization. Here is example of custom serialization I wrote:

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(); } }

在你的情况我会写我的对试剂类

In your case I would write my custom serialization on the level of Reagent class

更多推荐

存储复合/嵌套的对象图

本文发布于:2023-10-24 20:14:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1524895.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!