开发者

Db4o Tree structure C#

开发者 https://www.devze.com 2022-12-18 19:09 出处:网络
I\'m new to db4o and i\'m trying to figure out if it\'s possible to do the following: public class Page

I'm new to db4o and i'm trying to figure out if it's possible to do the following:

public class Page
{ 
    public Guid ID {get;set;}
    public Page Parent {get;set;}
    public IList<Page> Children {get;set;}
    public String Name {get;set;}
    public String Depth {get;set;}
}

When I save the page i have it's parent only. IE

Page p1 = new Page() { 
    ID = Guid.NewGuid(),
    Name = "p1",
    Parent = null
};
Page p2 = new Page() { 
    ID = Guid.NewGuid(),
    Name = "p2",
    Parent = p1
};
Page p3 = new Page() { 
    ID = Guid.NewGuid(),
    Name = "p3",
    Parent = p1
开发者_C百科};

When i load up p1 is there anyway to populate the two children??


Db4O will load also the collection of children as soon as you load p1 from the datastore, so yes it's possible...


Well the easiest way to do this is actually to just use the property-call to wire things up. Like this:

public class Page
{ 
    private Page _parent;
    public Page Parent {
        get{return _parent;}
        set{
            value.Children.Add(this);
            this._parent = value;
        }
    }
    public IList<Page> Children {get;set;}

    // omitted the the other properties
}

As soon as you assign a Page-instance to Page.Parent, the Page is in the Page.Children-property.

Is this a solution?


Not sure about DB4O but you should consider using the IComposite pattern for this.

0

精彩评论

暂无评论...
验证码 换一张
取 消