简体   繁体   English

db4o对象更新

[英]Db4o object update

I'm using db4o for a simple app, with an embedded db. 我将db4o用于带有嵌入式db的简单应用程序。 When I save an object, and then change the object, is it suppose that db4o returns the changed object? 当我保存一个对象,然后更改该对象时,是否应该认为db4o返回了更改后的对象?

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? 我是否需要以任何特殊方式设置db4o容器? wrap it in commit calls? 将其包装在提交调用中? Thanks 谢谢

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. 假设A(localAim2!= localAim)的问题在于,您使用存储在数据库中的同一对象处理2种不同的内容。 Without Assuption A (ie, localAim2 == localAim), your data is allways consistent since you have only one object (referenced two times). 如果没有假设A(即localAim2 == localAim),则由于您只有一个对象(被引用了两次),因此数据始终是一致的。

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

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