惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
P
Privacy International News Feed
Security Latest
Security Latest
H
Hacker News: Front Page
T
Tenable Blog
The Hacker News
The Hacker News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Security @ Cisco Blogs
Project Zero
Project Zero
O
OpenAI News
AI
AI
Spread Privacy
Spread Privacy
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
G
GRAHAM CLULEY
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
C
CXSECURITY Database RSS Feed - CXSecurity.com
NISL@THU
NISL@THU
A
Arctic Wolf
T
Threat Research - Cisco Blogs
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Know Your Adversary
Know Your Adversary
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
U
Unit 42
S
Security Affairs
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
博客园 - 【当耐特】
T
The Exploit Database - CXSecurity.com
S
Schneier on Security
月光博客
月光博客
Engineering at Meta
Engineering at Meta
腾讯CDC
F
Full Disclosure
Cyberwarzone
Cyberwarzone
S
SegmentFault 最新的问题
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 司徒正美
The Cloudflare Blog

博客园 - 东成西就

重灾面前,我们有力量面对一切困难! 试一下 使用CAB中EventBroker碰到困难,暂记于此! 在.NET中动态编译与执行脚本文件 db4o学习笔记(五)、处理结构化对象上 db4o学习笔记(四)、db4o查询详解续 db4o学习笔记(三)、db4o查询详解 db4o学习笔记(一)、db4o概览 LightweightCTI架构设计(5) LightweightCTI架构设计(4) LightweightCTI架构设计(3) LightweightCTI架构设计(2) LightweightCTI架构设计(1) LightweightCTI开发实录(5)板卡适配器概述 LightweightCTI开发实录(4)系统如何工作 LightweightCTI开发实录(3)分析之前 系分考试准备的论文(6):论系统设计中对用户需求的把握 LightweightCTI开发实录(2)项目计划 LightweightCTI开发实录(1) 缘由
db4o学习笔记(二)、第一次亲密接触
东成西就 · 2006-11-10 · via 博客园 - 东成西就

  让我们以一个简单的例子作为db4o之旅的开始,通过这个例子展示如何存储、更新、加载以及删除一个只包括系统内置类型及字符串成员的简单对象实例,这个对象是一个存储了F1车手(Pilot)的相关信息如姓名及本赛季所取得积分的类。

  首先我们将创建一个类来存储这些信息,它看上去像这样:

 1namespace com.db4o.f1.chapter1
 2{
 3    public class Pilot
 4    {
 5        string _name;
 6        int _points;
 7        
 8        public Pilot(string name, int points)
 9        {
10            _name = name;
11            _points = points;
12        }

13        
14        public string Name
15        {
16            get
17            {
18                return _name;
19            }

20        }

21        
22        public int Points
23        {
24            get
25            {
26                return _points;
27            }

28        }
   
29        
30        public void AddPoints(int points)
31        {
32            _points += points;
33        }
    
34        
35        override public string ToString()
36        {
37            return string.Format("{0}/{1}", _name, _points);
38        }

39    }

40}

41

  请注意:在类定义的代码中没有包含任何db4o相关的代码。

  2.1、打开数据库

  为打开已有的或新建一个db4o数据库我们使用Db4o.OpenFile()函数,并为其传入一个特定路径的文件名作为参数,以此来获得特定的ObjectContainer实例。ObjectContainer对外就是一个数据库,也是我们操作db4o的主要接口。关闭ObjectContainer使用#.Close()函数,它将会关闭数据库文件并释放其占用的系统资源。

 1[accessDb4o]
 2ObjectContainer db=Db4o.OpenFile(Util.YapFileName);
 3try
 4{
 5    // do something with db4o
 6}

 7finally
 8{
 9    db.Close();
10}

  在接下来的例子中假设我们的环境有一个变量"db"来引用和存储数据库文件Util.YapFileName,并会在需要的时候自动的打开或关闭数据库。

  2.2、保存对象

  为了保存对象我们只需要在数据库对象上简单的调用Set()方法并传入适当的参数即可。

1[storeFirstPilot]
2Pilot pilot1 = new Pilot("Michael Schumacher"100);
3db.Set(pilot1);
4Console.WriteLine("Stored {0}", pilot1);

  好,我们再存入第二个车手对象。

1[storeSecondPilot]
2Pilot pilot2 = new Pilot("Rubens Barrichello"99);
3db.Set(pilot2);
4Console.WriteLine("Stored {0}", pilot2);

  2.3、加载对象

  db4o提供了三种不同的查询数据的方法,(1)、通过实例查询(QBE);(2)、db4o原生/本地化查询(NQ);(3)、SODA查询接口(SODA是一种通过数据库持久层进行的查询,查询语句被定义在字符串中,并通过持久引擎进行解释执行)。在这一节的例子中我将仅介绍QBE,而在日常的开发中你将对象存储在数据库中后,我们推荐使用db4o主要的查询接口NQ进行查询。

  当使用QBE进行查询时,我们需要为希望加载的数据而创建一个对象原型(prototypical ),db4o将会加载所有与原型相同类型(各成员字段不为默认值)的对象,返回的结果将会存储在ObjectSet对象实例中,我们使用一个"listResult"的函数来显示返回结果。

1public static void ListResult(ObjectSet result)
2{
3    Console.WriteLine(result.Count);
4    foreach (object item in result)
5    {
6        Console.WriteLine(item);
7    }

8}

  为了从数据库中加载所有的车手对象,我们提供了一个初始值为空的Polit原型对象。

1[retrieveAllPilotQBE]
2Pilot proto = new Pilot(null0);
3ObjectSet result = db.Get(proto);
4ListResult(result);

  请注意在提供的原型对象中车手的积分为0,而实际返回的车手对象中则没有包含积分为0的对象,这是因为对于int型字段的默认值为0。

  db4o也提供了一种便捷的方法来加载这些对象:

1[retrieveAllPilots]
2ObjectSet result = db.Get(typeof(Pilot));
3ListResult(result);

  在.NET 2.0中则可以通过泛型来处理:

IList <Pilot> pilots = db.query<Pilot>(typeof(Pilot));

  以下示例则是通过车手的名字或特定的积分来加载车手对象:

 1[retrievePilotByName]
 2Pilot proto = new Pilot("Michael Schumacher"0);
 3ObjectSet result = db.Get(proto);
 4ListResult(result);  
 5
 6// And to query for Pilots with a specific number of points:
 7
 8[retrievePilotByExactPoints]
 9Pilot proto = new Pilot(null100);
10ObjectSet result = db.Get(proto);
11ListResult(result); 
12

  在db4o中还有其它的一些查询方法,这将在以后的章节中深入的介绍。

  2.4、更新对象

  更新对象跟存储它们一样简单,实际上我们只需要在修改对象后再次调用同样的方法Set()就可以了。

1[updatePilot]
2ObjectSet result = db.Get(new Pilot("Michael Schumacher"0));
3Pilot found = (Pilot)result.Next();
4found.AddPoints(11);
5db.Set(found);
6Console.WriteLine("Added 11 points for {0}", found);
7RetrieveAllPilots(db);

  请注意,在调用Set()方法更新对象之前我们先进行了查询,这一点十分重要。如果在当前的存储或加载操作过程所处的会话中对象对于db4o不可知的话,db4o将会向数据库中插入一个的对象,之所以会这样是因为db4o不会自动的去匹配先前存储于数据库的对象,而是假设你向数据库中存入第二拥有相同属性的对象。   

  为了验证数据库是否确实更新的车手信息,可以执行上面加载数据库对象的方法。

  2.5、更新对象

  删除数据库中的对象使用Delete()方法: 

1[deleteFirstPilotByName]
2ObjectSet result = db.Get(new Pilot("Michael Schumacher"0));
3Pilot found = (Pilot)result.Next();
4db.Delete(found);
5Console.WriteLine("Deleted {0}", found);
6RetrieveAllPilots(db);

  可以使用上面提供的加载对象的方法来验证其是否已经从数据库中真正的删除了。

  在更新对象时,对象也将首先被db4o删除,这时提供一个原型对象将不足于进行相关的操作而必须提供一个完全匹配的对象给数据库。

  2.6、小结

  是不是很简单呢?只需要简单的几行代码就完成了对db4o数据库对象的保存、更新、加载及删除操作。而对于复杂的查询任务如何完成呢?我们将通过在下一章中介绍的含有约束条件的QBE查询完成。 
  以下是本单元的所有源代码:

完整的代码