[英]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.