这是接着东成西就的翻译来的。
他的地址:http://www.cnblogs.com/JackyXu/archive/2006/11/10/556689.html(五)
http://www.cnblogs.com/JackyXu/archive/2006/11/10/557168.html(四)
http://www.cnblogs.com/JackyXu/archive/2006/11/10/557168.html(三)
http://www.cnblogs.com/JackyXu/archive/2006/11/13/559747.html(二)
http://www.cnblogs.com/JackyXu/archive/2006/11/13/559747.html(一)

4.3更新结构化对象

要更新db4o的结构化对象,只需要调用Set()方法。
db4o Tutorial 翻译(6)// updateCar
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(new Car("Ferrari"));
db4o Tutorial 翻译(6)Car found 
= (Car)result.Next();
db4o Tutorial 翻译(6)found.Pilot 
= new Pilot("Somebody else"0);
db4o Tutorial 翻译(6)db.Set(found);
db4o Tutorial 翻译(6)result 
= db.Get(new Car("Ferrari"));
db4o Tutorial 翻译(6)ListResult(result);
同时修改Pilot类:
db4o Tutorial 翻译(6)// updatePilotSingleSession
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(new Car("Ferrari"));
db4o Tutorial 翻译(6)Car found 
= (Car)result.Next();
db4o Tutorial 翻译(6)found.Pilot.AddPoints(
1);
db4o Tutorial 翻译(6)db.Set(found);
db4o Tutorial 翻译(6)result 
= db.Get(new Car("Ferrari"));
db4o Tutorial 翻译(6)ListResult(result);
很简单是不是?但是小心,有问题!如果我们将这个步骤(Session)拆成两个步骤(Session),将会如何?
首先,我们先修改Pilot,然后再修改他的Car
db4o Tutorial 翻译(6)// updatePilotSeparateSessionsPart1
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(new Car("Ferrari"));
db4o Tutorial 翻译(6)Car found 
= (Car)result.Next();
db4o Tutorial 翻译(6)found.Pilot.AddPoints(
1);
db4o Tutorial 翻译(6)db.Set(found);
然后,我们看一下结果:
db4o Tutorial 翻译(6)// updatePilotSeparateSessionsPart2
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(new Car("Ferrari"));
db4o Tutorial 翻译(6)ListResult(result);
看,有问题了!

4.3.1. 更新深度
想象一下,假如一个类包含很多子类,子类也包含子类。那么,当你修改这个类的对象的时候,要跟着一起更新很多类:他的子类、孙子类。。。。这样,无疑是低效率的。
所以,在刚才更新的步骤中,我们修改了Pilot的子类--Car。当我们保存修改时,我们叫db4o保存我们的Car对象,就认为db4o还帮我们修改了Pilot。但是,我们刚刚的修改程序和第一个修改程序一样,为什么第一次的修改程序可以真正修改到Pilot?第一次,我们修改后,db4o不会真正的获取修改的Pilot,他只是修改了内存中的同样的一个Pilot,而没有更新入数据库。其实我们看到的修改后的值是一个假象而已。重新运行程序,就会发现修改已经不存在了。
为了灵活的解决这个问题,db4o引进了“更新深度”这个概念,这个概念用来控制对一个对象成员树我们到底更新几层。默认值为1,表示只有原始的String型的变量被更新,而对内部引用的对象不做更新。
db4o让更新在一个“度”的范围内,不会过分,也不会浪费效率。但是我们现在想做的是让db4o更新整个Car的对象,而不是只更新一层。

db4o Tutorial 翻译(6)// updatePilotSeparateSessionsImprovedPart1
db4o Tutorial 翻译(6)
Db4oFactory.Configure().ObjectClass(typeof(Car))
db4o Tutorial 翻译(6)    .CascadeOnUpdate(
true);

 

db4o Tutorial 翻译(6)// updatePilotSeparateSessionsImprovedPart2
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(new Car("Ferrari"));
db4o Tutorial 翻译(6)Car found 
= (Car)result.Next();
db4o Tutorial 翻译(6)found.Pilot.AddPoints(
1);
db4o Tutorial 翻译(6)db.Set(found);

 

db4o Tutorial 翻译(6)// updatePilotSeparateSessionsImprovedPart3
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(new Car("Ferrari"));
db4o Tutorial 翻译(6)ListResult(result);


看起来好多了!

注意:设置更新深度的动作一定要写在db4o的Open()之前。
我们将会在以后的章节讲到db4o的更新深度、其他的复杂对象的重点,和各自db4o设置细节。

4.4. 删除结构化对象


就像我们之前用到的,Delete()方法可以删除一个对象。

db4o Tutorial 翻译(6)// deleteFlat
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(new Car("Ferrari"));
db4o Tutorial 翻译(6)Car found 
= (Car)result.Next();
db4o Tutorial 翻译(6)db.Delete(found);
db4o Tutorial 翻译(6)result 
= db.Get(new Car(null));
db4o Tutorial 翻译(6)ListResult(result);

很好,Car已经被删除了,那Pilot呢?
db4o Tutorial 翻译(6)// retrieveAllPilotsQBE
db4o Tutorial 翻译(6)
Pilot proto = new Pilot(null0);
db4o Tutorial 翻译(6)IObjectSet result 
= db.Get(proto);
db4o Tutorial 翻译(6)ListResult(result);
Ok, 这才是我们想要的结果 - 在真实生活中,当一个赛车手的car坏了的时候,我们不希望这个赛车手也消失。但是,当我们希望一个对象被删除了以后,他的子对象也被删除时候,该如何?

4.4.1. 循环删除


哈哈,你已经在猜想,是不是循环删除和循环更新很像呢?对了,让我们来设置db4o的删除深度。
db4o Tutorial 翻译(6)// deleteDeepPart1
db4o Tutorial 翻译(6)
Db4oFactory.Configure().ObjectClass(typeof(Car))
db4o Tutorial 翻译(6)    .CascadeOnDelete(
true);

db4o Tutorial 翻译(6)// deleteDeepPart2
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(new Car("BMW"));
db4o Tutorial 翻译(6)Car found 
= (Car)result.Next();
db4o Tutorial 翻译(6)db.Delete(found);
db4o Tutorial 翻译(6)result 
= db.Get(new Car(null));
db4o Tutorial 翻译(6)ListResult(result);

再次注意:所有的设置都不需在IObjectContainer 打开之前

让我们来看一下Pilot:
db4o Tutorial 翻译(6)// retrieveAllPilots
db4o Tutorial 翻译(6)
Pilot proto = new Pilot(null0);
db4o Tutorial 翻译(6)IObjectSet result 
= db.Get(proto);
db4o Tutorial 翻译(6)ListResult(result);

4.4.2. 再次回顾循环删除

 

问题--如果孩子对象被删除了,但是这个孩子还有被其他对象引用怎么办?

db4o Tutorial 翻译(6)// deleteDeepRevisited
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(new Pilot("Michael Schumacher"0));
db4o Tutorial 翻译(6)Pilot pilot 
= (Pilot)result.Next();
db4o Tutorial 翻译(6)Car car1 
= new Car("Ferrari");
db4o Tutorial 翻译(6)Car car2 
= new Car("BMW");
db4o Tutorial 翻译(6)car1.Pilot 
= pilot;
db4o Tutorial 翻译(6)car2.Pilot 
= pilot;
db4o Tutorial 翻译(6)db.Set(car1);
db4o Tutorial 翻译(6)db.Set(car2);
db4o Tutorial 翻译(6)db.Delete(car2);
db4o Tutorial 翻译(6)result 
= db.Get(new Car(null));
db4o Tutorial 翻译(6)ListResult(result);

 

db4o Tutorial 翻译(6)// retrieveAllPilots
db4o Tutorial 翻译(6)
Pilot proto = new Pilot(null0);
db4o Tutorial 翻译(6)IObjectSet result 
= db.Get(proto);
db4o Tutorial 翻译(6)ListResult(result);

我们有一个问题了:手边没有合适的解决方案。db4o在删除一个对象的时候,不会去检查是不是有别的对象引用了它,所以呢,要十分小心。
让我们在进入下一章以前清理一下数据库

db4o Tutorial 翻译(6)// deleteAll
db4o Tutorial 翻译(6)
IObjectSet result = db.Get(typeof(Object));
db4o Tutorial 翻译(6)
foreach (object item in result)
}


 

4.5. 总结


对象之间的关系有如下内容:我们可以钩住一个根对象,找到它的引用图,制定查询。但是当某个对象里面有像数组、集合等的多值成员的时候该怎么办?见下一章

4.6. Full source


 

db4o Tutorial 翻译(6)锘縰sing System;
db4o Tutorial 翻译(6)
using System.IO;
db4o Tutorial 翻译(6)
using Db4objects.Db4o;
db4o Tutorial 翻译(6)
using Db4objects.Db4o.Query;
db4o Tutorial 翻译(6)
namespace Db4objects.Db4o.Tutorial.F1.Chapter2



 

相关文章:

  • 2021-11-07
  • 2021-06-29
  • 2021-08-22
  • 2022-12-23
  • 2021-11-10
  • 2022-02-11
  • 2021-11-21
  • 2021-08-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
相关资源
相似解决方案