简体   繁体   English

db4o数据库用于自动测试

[英]Db4o database for automatic tests

we are using Db4o in our project. 我们在项目中使用Db4o。

I have some automatic tests where is tested persistence of objects. 我有一些自动测试,用于测试对象的持久性。 The problem is, I can't open/create the database twice. 问题是,我无法两次打开/创建数据库。 I have two helper methods for geting the object container. 我有两种获取对象容器的辅助方法。 But when the method is called second time the "ArgumentException: Configuration already used." 但是,第二次调用该方法时,“ ArgumentException:配置已使用”。 is thrown. 被抛出。 I closed and disposed the previous objectcontainer ofcourse. 我关闭并处置了之前的课程容器。

What I do wrong? 我做错了什么?

CODE: 码:

public static IObjectContainer GetEmptyTestingDatabase() {
    var tempDir = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
    string dbFilePath = Path.Combine(tempDir, "UNIT-TESTING.db4o");
    if (File.Exists(dbFilePath)) {
        File.Delete(dbFilePath);
    }

    var cfg = Db4oFactory.Configure();
    cfg.Add(new TransparentPersistenceSupport(new DeactivatingRollbackStrategy()));
    cfg.Add(new TransparentActivationSupport());

    var db = Db4oFactory.OpenFile(cfg, dbFilePath);
    return db;
}
public static IObjectContainer GetMemoryDatabase() {
    string dbFileName = Guid.NewGuid().ToString().ToString();

    var cfg = Db4oFactory.Configure();
    cfg.Storage = new Db4objects.Db4o.IO.PagingMemoryStorage();
    cfg.Add(new TransparentPersistenceSupport(new DeactivatingRollbackStrategy()));
    cfg.Add(new TransparentActivationSupport());

    var db = Db4oFactory.OpenFile(cfg, dbFileName);
    return db;
}

You are using deprecated db4o methods. 您正在使用不推荐使用的db4o方法。 The problem is that Db4oFactory.Configure() returns a static config object; 问题在于Db4oFactory.Configure()返回一个静态配置对象。 this method is there only for backward compatibility. 此方法仅用于向后兼容。

If you are using a newer db4o version use Db4oEmbedded.NewConfiguration() instead. 如果使用的是较新的db4o版本,请使用Db4oEmbedded.NewConfiguration()代替。 Otherwise (if you really need to stick to an older db4o version) using Db4oFactory.NewConfiguration() should also work. 否则(如果您确实需要坚持使用旧的db4o版本),则使用Db4oFactory.NewConfiguration()也应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM