且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

db4o 7.4 for .net3.5试用手记

更新时间:2022-08-27 14:03:55

db4o虽然出来很久了,一直没怎么关注,今天随手试了下,对于小型应用还是不错的,至少省去了ORM的麻烦,一切皆对象,一切都变得那么自然。

下载地址:http://www.db4o.com/DownloadNow.aspx

优点:
1.使用简单,整个引擎只要一个dll(Db4objects.Db4o.dll)即能搞定(个人觉得可完全取代access用于某些小型应用)

2.数据库仅一个文件,方便部署
3.支持事务(文档上有提到的,不过我没测试过)
4.性能还过得去(10w以内的数据还可以接受,详见我的测试结果)
5.彻底忘记ORM吧,一切皆Object,还要什么ORM?
6.可同时用于.net与java平台


下面是测试代码:
db4o 7.4 for .net3.5试用手记db4o 7.4 for .net3.5试用手记db4o测试 by 菩提树下的杨过 http://yjmyzz.cnblogs.com
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Diagnostics;
using Db4objects.Db4o;

namespace GetStarted
{
    
class Program
    {
        
static string DBFileName = "D:\\MyDoc\\db4o\\data\\demo.yap";

        
static void Main(string[] args)
        {
            
//如果文件已经存在,先删除(免得原来的记录影响测试结果)
            if (File.Exists(DBFileName)) { try { File.Delete(DBFileName); } catch (Exception e) { Console.WriteLine(e.Message.ToString()); } }

            IObjectContainer db 
= Db4oFactory.OpenFile(DBFileName);//打开数据库(如果数据库文件不存在,将自动创建)
            try
            {
                Pilot pilot1 
= new Pilot("Michael Schumacher"100);
                db.Store(pilot1);
//对象存入数据库(即传统意义上的insert操作)
                Console.WriteLine("Stored {0}", pilot1);

                Pilot pilot2 
= new Pilot("Rubens Barrichello"99);
                db.Store(pilot2);
//再存一个对象
                Console.WriteLine("Stored {0}", pilot2);

                Console.WriteLine(
"-----------------------------------------");

                Pilot proto 
= new Pilot(null0);//如果要查询出所有Pilot,创建一个"empty"的Pilot即可
                IObjectSet result = db.QueryByExample(proto);
                ListResult(result);

                Console.WriteLine(
"-----------------------------------------");

                Pilot proto1 
= new Pilot("Rubens Barrichello"0);//按Name查询(比如有意思的是:第二个参数随便什么都行,另外第一个参数是区分大小写的)
                IObjectSet result1 = db.QueryByExample(proto1);
                ListResult(result1);

                Console.WriteLine(
"-----------------------------------------");

                Pilot proto2 
= new Pilot(null99);//按points查询
                IObjectSet result2 = db.QueryByExample(proto2);
                ListResult(result2);

                Console.WriteLine(
"-----------------------------------------");

                IList
<Pilot> pilots = db.Query<Pilot>(typeof(Pilot));//泛型的查询方法,貌似写法更简单自然
                foreach (Pilot p in pilots)
                {
                    Console.WriteLine(p);
                }

                Console.WriteLine(
"-----------------------------------------");

                IList
<Pilot> pilots2 = db.Query<Pilot>(new Predicate<Pilot>(c => c.Name == "Rubens Barrichello"));//按Name查询
                foreach (Pilot p in pilots2)
                {
                    Console.WriteLine(p);
                }

                Console.WriteLine(
"-----------------------------------------");
                
//更新数据
                Pilot pilot3 = pilots2.FirstOrDefault();
                Console.WriteLine(
"before update:" + pilot3);
                pilot3.AddPoints(
10);
                db.Store(pilot3);
                var _p 
= db.Query<Pilot>(new Predicate<Pilot>(c => c.Name == pilot3.Name)).FirstOrDefault();
                Console.WriteLine(
"after update:" + _p);

                Console.WriteLine(
"-----------------------------------------");

                
//删除数据
                db.Delete(_p);
                IList
<Pilot> listAfterDel = db.Query<Pilot>(typeof(Pilot));//显示删除后的所有对象
                foreach (Pilot p in listAfterDel)
                {
                    Console.WriteLine(p);
                }

                Console.WriteLine(
"-----------------------------------------");

                
//大量数据插入测试                
                Stopwatch stopMatch = new Stopwatch();
                stopMatch.Start();
                
int Count = 100000;//10w条数据
                for (int i = 0; i < Count; i++)
                {
                    Pilot pInsert 
= new Pilot(i.ToString(),i);
                    db.Store(pInsert);
                }
                stopMatch.Stop();
                Console.WriteLine(
"{0}条记录插入共耗时{1}毫秒", Count, stopMatch.ElapsedMilliseconds);

                stopMatch.Start();
                IList
<Pilot> lstAll = db.Query<Pilot>(typeof(Pilot));
                stopMatch.Stop();
                Console.WriteLine(
"{0}条记录查询共耗时{1}毫秒", Count, stopMatch.ElapsedMilliseconds);

                stopMatch.Start();
                
foreach (Pilot p in lstAll)
                {
                    p.AddPoints(
1);
                    db.Store(p);
                }
                stopMatch.Stop();
                Console.WriteLine(
"{0}条记录更新共耗时{1}毫秒", Count, stopMatch.ElapsedMilliseconds);

                stopMatch.Start();
                
foreach (Pilot p in lstAll)
                {                  
                    db.Delete(p);
                }
                stopMatch.Stop();
                Console.WriteLine(
"{0}条记录删除共耗时{1}毫秒", Count, stopMatch.ElapsedMilliseconds);

            }
            
catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
            }
            
finally
            {
                db.Close();
            }

            Console.ReadLine();
        }


        
static void ListResult(IObjectSet result)
        {
            Console.WriteLine(result.Count);
            
foreach (object item in result)
            {
                Console.WriteLine(item);
            }
        }
    }


    
public class Pilot
    {
        
string _name;
        
int _points;

        
public Pilot(string name, int points)
        {
            _name 
= name;
            _points 
= points;
        }

        
public string Name { get { return _name; } }

        
public int Points { get { return _points; } }

        
public void AddPoints(int points)
        {
            _points 
+= points;
        }

        
public override string ToString()
        {
            
return string.Format("{0}/{1}", _name, _points);
        }
    }
}

本机运行结果:

Stored Michael Schumacher/100

Stored Rubens Barrichello/99

-----------------------------------------

2

Michael Schumacher/100

Rubens Barrichello/99

-----------------------------------------

1

Rubens Barrichello/99

-----------------------------------------

1

Rubens Barrichello/99

-----------------------------------------

Michael Schumacher/100

Rubens Barrichello/99

-----------------------------------------

Rubens Barrichello/99

-----------------------------------------

before update:Rubens Barrichello/99

after update:Rubens Barrichello/109

-----------------------------------------

Michael Schumacher/100

-----------------------------------------

100000条记录插入共耗时6176毫秒

100000条记录查询共耗时6189毫秒

100000条记录更新共耗时15861毫秒

100000条记录删除共耗时23730毫秒