存储复合/嵌套的对象图 [英] Storing composite/nested object graph

查看:156
本文介绍了存储复合/嵌套的对象图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发中蒙戈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

这篇关于存储复合/嵌套的对象图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆