In the last post in this series ("what's wrong with this code") we discussed how db4o's reference system remembers which objects have been stored / retrieved.
In this post I'll show you another piece of code that doesn't always work as the developer expects:
In this post I'll show you another piece of code that doesn't always work as the developer expects:
class User { public string Name; public User Manager; public IList Addresses; public User(string name, User manager) { Name = name; Manager = manager; } } class Address { public string Street; public Address(string street) { Street = street; } } using (var db = Db4oEmbedded.OpenFile(databaseFileName)) { List<Address> addrs = new List(); addrs.Add(new Address("Street1")); addrs.Add(new Address("Street2")); User fooManager = new User("Foo") { Addresses = addrs }; User fooBarManager = new User("FooBar", fooManager); User barManager = new User("John Doe", fooBarManager); User user = new User("Adriano", barManager); db.Store(user); } using (var db = Db4oEmbedded.OpenFile(databaseFileName)) { var query = db.Query(); query.Constrain(typeof (User)); query.Descend("Name").Constrain("Adriano"); User user = (User) query.Execute()[0]; Console.WriteLine(user.Manager.Manager.Manager.Addresses[0].Street); }
Can you see the problem? No, I am not talking about Law of Demeter "violation" :) (By the way, if you are interested in this subject you can find a nice discussion here).
Again, if you have been working with db4o for some time I am sure you already spent some time debugging this kind of issue.
Again, if you have been working with db4o for some time I am sure you already spent some time debugging this kind of issue.
As always, the answer comes in a future post.
Have fun!
Adriano