繁体   English   中英

db4o对象更新

[英]Db4o object update

我将db4o用于带有嵌入式db的简单应用程序。 当我保存一个对象,然后更改该对象时,是否应该认为db4o返回了更改后的对象?

这是代码:

[Test]
    public void NonReferenceTest()
    {
      Aim localAim = new Aim("local description", null);
      dao.Save(localAim);

      // changing the local value should not alter what we put into the dao
      localAim.Description = "changed the description";

      IQueryable<Aim> aims = dao.FindAll();

      var localAim2 = new Aim("local description", null);
      Assert.AreEqual(localAim2, aims.First());
    }

测试失败。 我是否需要以任何特殊方式设置db4o容器? 将其包装在提交调用中? 谢谢

实际上,它应该以这种方式工作。 您必须记住,您不仅在操作对象,而且还处理数据。

当向(或从)对象数据库存储(或查询)对象时,它会将存储的数据与对象之间的链接保持在内存中。 当您更新对象并将其存储在数据库中时,这是必需的。 实际上,您不希望存储新对象,但是希望更新旧对象。 因此,当检索仍然存在于内存中的对象时,将为您提供对该对象的引用。

另一个原因是数据完整性。 再次查看您的代码,想象一下它为您提供了数据库数据,而不是对更新对象的引用:

Aim localAim = new Aim("local description", null);
dao.Save(localAim);

// changing the local value should not alter what we put into the dao
localAim.Description = "changed the description";

IQueryable<Aim> aims = dao.FindAll();

var localAim2 = aims.First();

// Assuption A: localAim2 != localAim
localAim2.Description += " added s/t";

dao.Save(localAim); 
// with Assuption A you now have "changed the description" in database
dao.Save(localAim2);
// with Assuption A you now have "local description added s/t"

假设A(localAim2!= localAim)的问题在于,您使用存储在数据库中的同一对象处理2种不同的内容。 如果没有假设A(即localAim2 == localAim),则由于您只有一个对象(被引用了两次),因此数据始终是一致的。

暂无
暂无

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

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