using System; using System.IO; using System.Linq; using Db4objects.Db4o; using Db4objects.Db4o.Linq; namespace Db4OSeries { internal class Program { private static void Main(string[] args) { File.Delete("database.db4o"); using (var db = StartDataBase()) { StoreMates(db); } using (var db = StartDataBase()) { var person = LoadMius(db); try { WithoutActivation(person); } catch (NullReferenceException e) { Console.Out.WriteLine("Exception thrown because we accessed a unactivated object"); } WithoutActivation(person, db); Console.Out.WriteLine("Press any key to end"); LostUpdate(person, db); } using (var db = StartDataBase()) { var person = LoadMius(db); LostUpdate(person, db); } using (var db = StartDataBase()) { var person = LoadMius(db); Console.Out.WriteLine(person.SirName); Console.Out.WriteLine(person.Boss.SirName); } Console.ReadKey(); } private static Person LoadMius(IEmbeddedObjectContainer db) { return (from Person p in db where p.FirstName.Equals("Mius") select p).Single(); } private static void LostUpdate(Person person, IObjectContainer db) { person.SirName = "Cool-NewName"; person.Boss.SirName = "Cool-NewBoss"; // doesn't work, because in doesn't traverse over the boss-reference db.Store(person); // instead use // db.Ext().Store(person, 2); // or store explicit the boss aswell // db.Store(person.Boss); } private static void WithoutActivation(Person person) { while (null != person) { // on the last person 'Julius Caesar' properties are null, hence this will fail Console.Out.WriteLine( string.Format("Persons initials {0}.{1}.", person.FirstName[0], person.SirName[0])); person = person.Boss; } } private static void WithoutActivation(Person person, IObjectContainer db) { while (null != person) { db.Activate(person, 1); Console.Out.WriteLine( string.Format("Persons initials {0}.{1}.", person.FirstName[0], person.SirName[0])); person = person.Boss; } } private static IEmbeddedObjectContainer StartDataBase() { return Db4oEmbedded.OpenFile(Db4oEmbedded.NewConfiguration(), "database.db4o"); } private static void StoreMates(IObjectContainer container) { var ceo = new Person("Julius", "Caesar"); var firstGeneral = new Person("NoIdea", "WhoCareus", ceo); var secondGeneral = new Person("Master", "Chiefius", firstGeneral); var captain = new Person("Biggest", "Halo-Geekius", secondGeneral); var officer = new Person("Plus", "Nixus", captain); var normalPerson = new Person("Mius", "Dius", officer); container.Store(normalPerson); } } }