db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join

Re: Does db4o support lazy loading?

  •  07-02-2007, 01:33 PM

    Re: Does db4o support lazy loading?

    Thank you for your reply!

    However despite my best efforts I am still unable to reproduce the behaviour the links, you provided, explain or at the very least unable to verify that it is taking place. I've tried setting ActivationDepth to both 0 and 1 and set the EvaluationMode to Lazy and from what I can tell my entire object graph is still loaded

     My object graph is very simple

    Forum - (threads) -> Thread -> (posts) -> Post

    Both Thread and Post have a Parent property. Below is a very basic sample that I cant get it to work. I have a follow up question but I wont ask it until this problem has been solved as it would only add to the (my) confusion and the solution to this problem might give me a reply on the other question

     

    using System;

    using System.Collections.Generic;

    using System.Text;

    using Db4objects.Db4o;

    using Db4objects.Db4o.Config;

    using Db4objects.Db4o.Query;

    namespace db40Optimizations

    {

    class Program

    {

    static void Main(string[] args)

    {

    /*

    * RUN THIS ONCE TO CREATE THE EXAMPLE OBJECTS

    */

    //Forum f = Forum.Create();

    //if (f != null)

    // Console.WriteLine("Storing objects: {0}", Utils.StoreObjects(f));

    IObjectContainer database = null;

    try

    {

    database = Db4oFactory.OpenFile(Utils.DatabaseName);

    Db4oFactory.Configure().ActivationDepth(0);

    Db4oFactory.Configure().Queries().EvaluationMode(QueryEvaluationMode.LAZY);

    IList<Forum> forums =

    database.Query<Forum>();

    Forum f = forums[0];

     

    /*

    * IF I INSPECT THE f OBJECT, THE THREADS COLLECTION (AND IT'S CHILDREN)

    * WILL ALL HAVE BEEN LOADED (NO NULL VALUES) DESPITE WHAT ACTIVATIONDEPTH

    * I'VE CONFIGURED (0 or 1) AND EVEN THOUGH EVALUATIONMODE IS SET TO LAZY

    */

    }

    catch (Exception e)

    {

    Console.WriteLine(e.ToString());

    }

    finally

    {

    database.Close();

    }

    Console.WriteLine("Press any key to quit...");

    Console.ReadLine();

    }

    }

    class Utils

    {

    public static string DatabaseName = "mydb.yap";

    public static bool StoreObjects(Forum forumObj)

    {

    IObjectContainer db = null;

    try

    {

    db = Db4oFactory.OpenFile(Utils.DatabaseName);

    db.Set(forumObj);

    return true;

    }

    catch (Exception e)

    {

    Console.WriteLine(e.ToString());

    }

    finally

    {

    db.Close();

    }

    return false;

    }

    }

    class Forum

    {

    public Forum()

    {

    }

    public Forum(string name, string description)

    {

    this.Name = name;

    this.Description = description;

    }

    private string description;

    public string Description

    {

    get { return description; }

    set { description = value; }

    }

     

    private string name;

    public string Name

    {

    get { return name; }

    set { name = value; }

    }

    private List<Thread> threads = new List<Thread>();

    public List<Thread> Threads

    {

    get { return threads; }

    set { threads = value; }

    }

    public static Forum Create()

    {

    Forum forumObj = new Forum(Guid.NewGuid().ToString("D"), Guid.NewGuid().ToString("D"));

    for (int i = 0; i < 10; i++)

    {

    Thread threadObj =

    Thread.Create(forumObj);

    forumObj.Threads.Add(threadObj);

    }

    return forumObj;

    }

    }

    class Thread

    {

    public Thread()

    {

    }

    public Thread(string subject)

    {

    this.Subject = subject;

    }

    private Forum parent;

    public Forum Parent

    {

    get { return parent; }

    set { parent = value; }

    }

     

    private List<Post> posts = new List<Post>();

    public List<Post> Posts

    {

    get { return posts; }

    set { posts = value; }

    }

    private string subject;

    public string Subject

    {

    get { return subject; }

    set { subject = value; }

    }

    public static Thread Create(Forum parent)

    {

    Thread threadObj = new Thread(Guid.NewGuid().ToString("D"));

    for (int i = 0; i < 10; i++)

    {

    Post PostObj = Post.Create(threadObj);

    threadObj.Parent = parent;

    threadObj.Posts.Add(PostObj);

    }

    return threadObj;

    }

    }

    class Post

    {

    public Post()

    {

    }

    public Post(string author, string contents)

    {

    this.Author = author;

    this.Contents = contents;

    this.Created = DateTime.Now;

    }

    private string author;

    public string Author

    {

    get { return author; }

    set { author = value; }

    }

     

    private string contents;

    public string Contents

    {

    get { return contents; }

    set { contents = value; }

    }

    private DateTime created;

    public DateTime Created

    {

    get { return created; }

    set { created = value; }

    }

    private Thread parent;

    public Thread Parent

    {

    get { return parent; }

    set { parent = value; }

    }

    public static Post Create(Thread parent)

    {

    Post postObj =

    new Post(Guid.NewGuid().ToString("D"), Guid.NewGuid().ToString("D"));

    postObj.parent = parent;

    return postObj;

    }

    }

    }

View Complete Thread