Db4o 对象更新

发布于 2024-08-04 02:44:39 字数 590 浏览 11 评论 0原文

我将 db4o 用于一个简单的应用程序,带有嵌入式数据库。当我保存一个对象,然后更改该对象时,是否假设 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 容器吗?将其包装在提交调用中?谢谢

I'm using db4o for a simple app, with an embedded db. When I save an object, and then change the object, is it suppose that db4o returns the changed object?

Here's the code:

[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());
    }

The test fails. Do I need to setup the db4o container in any special way? wrap it in commit calls? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喵星人汪星人 2024-08-11 02:44:39

事实上,它应该是这样工作的。您必须记住,您操作的是对象而不仅仅是数据。

当将对象存储到对象数据库(或从对象数据库查询)时,它会保留存储的数据与内存中的对象之间的链接。当您更新对象并将其存储在数据库中时需要这样做。您实际上不希望存储新对象,但希望更新旧对象。
因此,当检索内存中仍然存在的对象时,您将获得对该对象的引用。

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

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),您的数据始终是一致的,因为您只有一个对象(引用两次)。

Actually, it is supposed to work that way. You have to keep in mind that you are manipulating objects not only data.

When storing (or querying) an object to (or from) the object-database, it keeps the link between the stored data and the object in memory. This is needed when you update the object and store it in the database. You actually do not want that a new object is stored but you want the old object to be updated.
So, when retrieving an object that still exists in memory, you will be given a reference to that object.

Another reason is data integrity. Look at your code again, and imagine it gives you the data of the database and not a reference to the updated object:

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"

The problem with Assuption A (localAim2 != localAim) is that you work on the same object that is stored in the database with 2 different contents.
Without Assuption A (i.e., localAim2 == localAim), your data is allways consistent since you have only one object (referenced two times).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文