1

小さな問題があります別のオブジェクトを含むオブジェクトを保存する方法は? 例えば

対象者

Person { Name, Mother }

ここで、Mother は同じクラス Person の別のオブジェクトです

thx ヘルプ lczernik

4

2 に答える 2

1
class Person
{
    public string Name;
    public Person Mother;
    Person (string name, Person mother)
    {
        Name = name;
        Mother = mother;
    }
}

のように使う

Person me = new Person("user2069747", new Person("user2069747's mom name", null));
// null was used because you may not know the name of your grand mom;
Console.WriteLine(me.Name) // prints your name
Console.WriteLine(me.Mother.Name) // prints your mom's name
于 2013-02-13T20:01:25.467 に答える
0

db4o はオブジェクト グラフ全体を自動的に保存しますが、パフォーマンスの最適化のため、次のシナリオに注意する必要があります。

  • オブジェクト検索
  • オブジェクトの更新

どちらの場合も、db4o は、次の構成パラメーターで設定された特定の深さまで要求された操作を実行します。

したがって、次のモデルを指定すると、オブジェクト グラフを 1 回の呼び出しで格納できます。

using System;
using Db4objects.Db4o;

namespace Db4oSample
{
    class Person
    {
        public string Name { get; set; }
        public Person Mother { get; set; }

        public override string ToString()
        {
            return Name + (Mother != null ? "(" + Mother + ")" : "");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var grandMother = new Person {Name = "grandma"};
            var mother = new Person {Name = "mother", Mother = grandMother };
            var son = new Person {Name = "son", Mother = mother};

            using(var db = Db4oEmbedded.OpenFile("database.odb")) // Closes the db after storing the object graph
            {
                db.Store(son);
            }

            using(var db = Db4oEmbedded.OpenFile("database.odb"))
            {
                var result = db.Query<Person>(p => p.Name == "son");
                if (result.Count != 1)
                {
                    throw new Exception("Expected 1 Person, got " + result.Count);
                }

                Console.WriteLine(result[0]);
            }
        }
    }
}

お役に立てれば

于 2013-02-14T11:52:40.833 に答える