tags:

views:

92

answers:

3

Hi Everyone

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

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

+1  A: 

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?

Gamlor
Added this in.. question tho.. if i put Children = new List<Page>() into my constructor does this effect Db4o when it actives the object on load?
ChrisKolenko
Normally it doesn't. By default db4o creates an 'empty' instance of the class via reflection without using the constructor. And then it puts the values into this instance, again via reflection. It's possible to configure db4o in such a way, that it calls the constructors: http://developer.db4o.com/Documentation/Reference/db4o-7.12/net35/reference/html/reference/tuning/performance_hints/calling_constructors.htmlAdditionally on some embedded, more limited platforms the constructor is also called.
Gamlor
A: 

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

Mac
I like this pattern makes sense.. I'm going to implement it.. thanks for bringing it to my attention
ChrisKolenko
+1  A: 

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

Tim Mahy