Can I query db4o for all objects implementing some interface?
i have a problem with db4o and I wanna know is it feature or bug.
Let's see some code
private interface IInterfac开发者_如何学JAVAe {}
private class SimpleObject : IInterface
{}
[TestMethod, Ignore]
public void _()
{
var replicableServer = Db4oFactory.OpenServer(Path.GetFullPath(@"testdb"), 777);
try
{
replicableServer.GrantAccess("user", "user");
var client2 = Db4oFactory.OpenClient("127.0.0.1", 777, "user", "user");
var client1 = Db4oFactory.OpenClient("127.0.0.1", 777, "user", "user");
client1.Store(new SimpleObject());
client1.Commit();
var query = client2.Query();
query.Constrain(typeof(IInterface));
Assert.AreEqual(1, query.Execute().Count);
}
finally
{
replicableServer.Close();
}
}
Here we have failed assert. But if we change type in constraint to SimpleObject, all would work fine. This is strange and I can't find reason to this.
Thank guys. But we solved this riddle. Problem was, that db4o saving information about object and what interface it implement, only after first save.
So, we simple saved all our empty objects to base, before work with it.
I don't know exactly how db4o works, but having written a document db, I can say it's unlikely that db4o stores the entire type hierarchy. It will only keep track of the type that was used to store the data. This is really only for serialization in my case.
To make something like this work, you would likely need to build your own index. When an object is stored, for each type in the hierarchy (except Object
and IDisposable
I expect) update a record like new TypeIndex { IndexedType = typeof(IInterface), ActualKey = ... }
.
精彩评论