Posts Tagged ‘db4objects’


I was looking around for alternatives for databases, and came to the idea of “Object Oriented Databases”, and I remember seeing something about this when I was in college and then it disappeared and I did not hear from it till a couple of years ago, and more recently with the “NoSQL” movement.
So the concept is quite simple. You create your classes, instantiate your objects, then you “Open” a connection to your database, and store the object in it.
The advantage is that, all the breaking apart of your objects, that would happen in a “Relational Database”, does not have to happen since your objects are going to be stored as they are. What I mean by that is that a situation where you have a parent table and a child table, would be represented in object as a instance of the parent with a property (Collection) of the children, that when stored in the database would then be broken into one row in the parent table and 0-N rows in the child table. Then when retrieved that process would have to be reversed into the materialization of a object with children again, and this will result in loss of speed.
The other advantage is that when changes are required they are easier to be implemented, and will be done with minor changes to the code.
The DBMS that seems to have the biggest support on the market today is db4objects by Versant, also it seems to be very mature as well with about 11 year on the market. I will be using the .NET 4 version of the database on windows forms.
I will try to illustrate, the process from beginning to end, then I will do some analyses of the supposed benefits.
Downloading and Installing
The official web download page is http://www.db4o.com/DownloadNow.aspx you can go there an get the .NET for version, I strongly encourage you to sign up for the community so you can get support and answers from the people using it.
It’s a small download of about 23mb and should be no trouble.  Run the msi, and configure it as you please, when you finish the installation, go to Start Menu –> db4objects –> db4o 8.0 –> Install ObjectManager VS plugin, this should install a little bar to VS, that allows you to manage your database.
One its all installed you should open your VS and start a new project, then you have 2 options:
  1. Manually reference the DLLs from your install folder
    1. Db4objects.Db4o.dll
    2. Db4objects.Db4o.Data.Services.dll
    3. Db4objects.Db4o.Linq.dll
  2. Use nuget (http://www.nuget.org/)
    1. If you have not installed it yet, do it. (What you waiting for? Zombies?
    2. There are different way of installing packages, but the easiest is
      1. Right click on references folder
      2. Add Library Package Reference
      3. On the left panel click “Online”
      4. Then search for “db4o”
      5. pick “db4o-devel” and hit install
That’s all there is to it, you have all you need installed and configured now you can start your development.
Creating and Opening the Database
To create the database, you will have to use the following line (ex: 1).
ex: 1
IObjectContainer db = Db4oEmbedded.OpenFile(“TestDB”);
This will create the file if it does not exist or it will open if it does exist.
Creating and Storing Objects
Considering the classes Author (ex: 2), Book (ex: 3).
ex: 2
public class Author
{
public string Name { get; set; }
public Guid? Id { get; set; }
}
ex: 3
public class Book
{
public string ISBN { get; set; }
public string Title { get; set; }
public Author Author { get; set; }
}
Once you instantiate the container, you then, can create your object as you would normally, then you call a method called Store() from your container passing as parameter the new object (ex: 4).
ex: 4
Author author = new Author();
author.Id = Guid.NewGuid();
author.Name = “Mateus de Carvalho”;
db.Store(author);
Book book = new Book();
book.ISBN = “VERYAWSOMEISBN”;
book.Title = “Book of Awesome”;
book.Author = author;
db.Store(book);
Retrieving the Objects
There are different ways of getting your objects, one way it to use LINQ.
Get all authors (ex: 5)
ex: 5
var resultQuery = from a in db.Query<Author>()
select a;
Get all books for a author (ex: 6), where the author is one of the objects from the list returned above.
ex: 6
var resultQuery = from b in db.Query<Book>()
where b.Author == author
select b;
Now we will use the type as way to get all authors (ex: 7).
ex: 7
IObjectSet result = db.QueryByExample(typeof(Author));
Now for a given author books, you create a object of book, fill in the properties that you want to match, in this case, the author, and the you execute the query (ex: 8).
ex: 8
Book book = new Book { ISBN = null, Title = null, Author = author };
IObjectSet result = db.QueryByExample(book);
Working with Array Properties
Lets say that you want to sell those books, and to do that you are going to create a Order class (ex: 9), and in that class you will have a list of books you sold. Now the rest you know the drill, create a instance of Order, then for each Book that is sold you add to the Books property of that order (ex: 10).
ex: 9
public class Order
{
public Guid? Id { get; set; }
public DateTime Date { get; private set; }
public int Count
{
get
{
return this._books.Count;
}
}
private IList _books;
public IList Books
{
get
{
return this._books;
}
}
public Order() : this(null, DateTime.Now, new ArrayList())
{
}
public Order(Guid? id, DateTime date, IList books)
{
Id = id;
this.Date = date;
this._books = books;
}
}
ex: 10
ArrayList booksSale = new ArrayList();
booksSale.Add(book1); // Selection 1
booksSale.Add(book2); // Selection 2
booksSale.Add(book3); // Selection 3

Order order = new Order(Guid.NewGuid(), DateTime.Now, booksSale);
db.Store(order);
Then one you sold you want to find the orders based on a given book (ex: 11).
ex: 11
var resultQuery = from o in db.Query<Order>()
where o.Books.Contains(book)
select o;
dgvSales.DataSource = null;
dgvSales.DataSource = resultQuery.ToList();
dgvSales.Refresh();
That gives you a nice idea of how this works, and you can look further for more information in the documentation that is provided with the download you did, and in the community.
Updating Objects
Updating objects is actually quite simple, first you find the object, by one of the ways we talked about already, then you make your property changes and store it again. The engine will make the proper procedure to store the changes to that object (ex: 12).
ex: 12
var author = (from a in db.Query<Author>()
where a.Name = “Mateus Carvalho”
select a).SingleOrDeafult();
author.Name = “Mateus de Carvalho”;
db.Store(author);
Deleting Objects
Well you probably guessed it already, but I will say it any ways. First you retrieve it by any of the means described above (kind of repetitive, hum?), then you call Delete, I knew you knew it (ex: 13).
ex: 13
var author = (from a in db.Query<Author>()
where a.Name = “Jhon Smith”
select a).SingleOrDeafult();
db.Delete(author);
Analyses
Now that we know the basics I will do a couple of things, to see how is the performance on this.
What I will do is as follows
  1. Insert 10.000 Authors
  2. Insert 10.000 Books
  3. Find 3 Books and make 10.000 orders
  4. Repeat steps 1 – 3 in the same database
Time (ms) Time(s) Container
1 813 0.8 Single Use
2 1400 1.4 Single Use
3 5937 5.9 Single Use
4.1 600 0.6 Same
4.2 1982020 1682 Same
4.3 3421 3.4 Same
The testing was done using the windows application (64bit) available with the post, I used the Parallel library to speed up a little, and the machine is a I7, with 6Gb.
Notice in item 4.2 that the is actually really high, I haven’t optimized the code, but the basic reason is that each book inserted I first search the database for a specific author, then create the book for him.
I did not optimize that part of the code, because the situation is highly improbable and I still wanted to see the outcome. A action scenario, the User would choose a author then add a book to that author.
If you are processing data in memory I would recommend, preparing the “data set” before starting the processing, in this you will not be constrained by IO speed.
Obs: If you are going to process a really large amount of data, remember that .NET 32bits will have a limit on the number of handles it can use, which will cause an OutOfMemmoryException. To go around this problem you can change you application to run under 64bits and it should stop, but eventually you will consume you ram and the process is going to slow down a lot.