面向对象数据库Perst使用入门
我比较热忠于面向对象数据库的原因是使用面向对象数据库时对数据的存储不需要编写(或编写量很小)额外代码来进行数据的存储及读取。特别是在很多情况下存储与读取的数据量很小,其实我们并不关心效率(实际上根据测试面向对象数据库的效率也很好)。此时面向对象数据库就有很大用处了。
有人说可以用JSON和XML,其实我之前也是用这些东西来做数据存储的。但是这两者所需要的代码也不少(感觉这两者更像比较简单的关系数据库)。特别是从头开发,甚至需要写专门的类来进行这数据的操作,使用了一段时间我就开始找新的东西了。
下面实现了Prest最简单的增删改查,当然,Prest还有更聪明,效率更高的方法。这里仅仅是演示一个用法,给大家一个初步的概念。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
using Perst; using System; namespace PrestTest2 { public class DepartMent : Persistent { public string name; public string location; } public class Employee : Persistent { public string name; public DepartMent inDeparMent; } class Program { static void skip(string prompt) { Console.Write(prompt); Console.ReadLine(); } static string input(string prompt) { while (true) { Console.Write(prompt); String line = Console.ReadLine().Trim(); if (line.Length != 0) { return line; } } } static long inputLong(string prompt) { while (true) { try { return Int32.Parse(input(prompt)); } catch (FormatException) { Console.WriteLine("Invalid integer constant"); } } } static double inputDouble(string prompt) { while (true) { try { return Double.Parse(input(prompt)); } catch (FormatException) { Console.WriteLine("Invalid floating point constant"); } } } static public void Main(String[] args) { //打开数据库并创建表格 Storage storage = StorageFactory.Instance.CreateStorage(); storage.Open("testssd4.dbs"); Database db = new Database(storage); db.CreateTable(typeof(DepartMent)); db.CreateTable(typeof(Employee)); DepartMent dep1 = new DepartMent(); dep1.name = "Jave Dep"; dep1.location = "301"; DepartMent dep2 = new DepartMent(); dep2.name = "C# Dep"; dep2.location = "302"; //增加记录 db.AddRecord(dep1); db.AddRecord(dep2); //操作完成后要记得Commit,会将数据送入到数据库 storage.Commit(); Employee perter = new Employee(); perter.name = "Perter"; Employee mark = new Employee(); mark.name = "Mark"; Employee robert = new Employee(); robert.name = "Robert"; DepartMent dep_c = new DepartMent(); foreach (DepartMent dep in db.GetRecords(typeof(DepartMent))) { if (dep.name == "Jave Dep") { //修改数据 dep.name = "Javab Dep"; db.AddRecord(dep); perter.inDeparMent = dep; mark.inDeparMent = dep; dep_c = dep; } if (dep.name == "C# Dep") { robert.inDeparMent = dep; } } db.AddRecord(perter); db.AddRecord(mark); db.AddRecord(robert); storage.Commit(); dep_c.name = "changed"; //修改数据 db.AddRecord(dep_c); foreach (DepartMent dep in db.GetRecords(typeof(DepartMent))) { Console.WriteLine("Dep name: " + dep.name + ", supplier.location: " + dep.location); } foreach (Employee emp in db.GetRecords(typeof(Employee))) { Console.WriteLine("Employee Name: " + emp.name + ", Dep: " + emp.inDeparMent.name); } storage.Commit(); //注意操作完成的后关闭Storage storage.Close(); skip("Press ENTER to continue"); } } } |
代码主要的内容:
1、代码主要实现了两个类,Department(部门)和Employee(员工),两者之间有一个一对多的关系(一个部门有多个总工)。这两个类除了继承于Prest.Persistent之外没有其任何的特殊。下面对这个基类作一个简单的说明。
2、Prest的官方说明文档是这样解释Prest.Persistent类的:
Perst consists of a Persistent class that is the base?class for all persistence-capable objects and provides methods for loading/locking and?storing objects;
这段话的意思是如果你想要把类的内容存储在数据库中,就需要继承Prest.Persistent类,这个类可以帮助你处理关于数据库的相关事宜(加载、锁定、存储)。
3、关于可以存储在数据库中的类Prest的官方文档也有如下说明:
Perst provides orthogonal persistence - i.e. any database class is persistence-capable (may be stored in the database). But to achieve best performance and flexibility, it is preferable for any persistence-capable class to implement IPersistent interface.
The default implementation of this interface is the Perst.Persistent class. In most cases, persistence-capable classes should be derived from the Persistent base class (or from some other persistence-capable class). An alternative implementation of the IPersistent interface may be needed if application classes must be derived from some other external class for which sources are not available. In such cases, the solution is to provide an implementation derived from the required base class and reuse Persistent.cs code to implement methods of the IPersistent interface.
这一段的意思是所有的类都可以存储在Perst数据库中,但如果你想要达到最好的效果,最好继承IPersistent这个接口。通常情况下你只要你的类继承于Presistent类就可以了。在一些特殊情况下你也可以实现IPersistent接口(接口的好处和坏处就不用我多说了吧)。
4、Perst存储的结构:主要有三层,第一层是Storage(实际存在是一个文件,可以是硬盘上的一个文件,也可以是内存中的一个文件)、第二层是Database(与关系数据库中的Database相同)、第三层是Table(可以类比关系数据库中的表,但每一个表就是类的存储集合,只是用Table的概念使之更容易理解)。所以我们用下面的代码来打开数据库:
1 2 3 4 5 6 |
Storage storage = StorageFactory.Instance.CreateStorage(); storage.Open("testssd4.dbs"); Database db = new Database(storage); db.CreateTable(typeof(DepartMent)); db.CreateTable(typeof(Employee)); |
大家可能会感觉Storage和Database两者作用比较相近,事实上Database和Table是为是给大家提供与关系型数据库相类似的编程体验而设计的。Database类可以看作是一系列功能的集合,这些功能非常有利于从关系型数据库迁移过来的用户使用。官方文档中介绍Database主要提供了四项功能:a、维护表格。b、维护索引。c、表格层次的事件。d、与SQL类似的查询语言。所以Database类是非常适合初学者使用的。至于更加“面向对象的方法”,后续的文章会介绍。
5、关于增删改查的功能。本文只实现了非常初级的增删改查的功能,而且查询的功能非常笨拙,其他更高明的方法后续文章也会介绍。
说点什么