Lately, I have fiddled around with Versant’s excellent dual-license object database db4o a little and I think object databases are very neat. In fact, I read a few pages in PoEAA [Patterns of Enterprise Application Architecture] today, and here and there Fowler writes how cool object databases are. The Fowler knows!
Well, anyhow when you download db4o (sign up and go for the latest release) there is no standalone server application included. That makes perfect sense, because the server needs access to your domain model / data model dll. In an object database, there are no tables, but there are objects, so you have to compile them into the application that runs the actual db4o server. There is server code which handles practically everything that is tricky, but it’s in a dll so you’ll have to write a few lines to build an application out of it. I figured this would be a great time to write my first windows service and so I mashed up some code I found on the net.
I initially thought the VS 2008 designer would set up pretty much everything, but it turned out that things are a little more complicated.
Since a service will run in a different manner, potentially using a different user account, etc. it needs to be installed first. There is a tool called
installutil.exe, but using it is a little awkward. Also, you’d have to deploy it along your application. Matt Davis posted a cool guide on stackoverflow and some additional information here on how to write a windows service that is esentially able to install itself!
One of the most peculiar features is that -should you prefer to have your own event log instead of writing to the ApplicationLog- you have to search through a tree of installers, find the
EventLogInstaller and modify it instead of simply adding a new one because the
ServiceInstaller will come along with the
EventLogInstaller as a child:
EventLogInstaller installer = FindInstaller(this.Installers);
if (installer != null)
installer.Log = ServiceConfiguration.LogName;
We also need to take care of spawning the actual worker thread, but fortunately there is a comprehensive guide on MSDN explaining this.
Using the Sample
First things first: Of course, this is not nearly production-safe code! Also, I did not include the db4o dll’s because I’m not sure about the licence terms.
The installer in 'action'. Note the Administrator prompt
- Download the sample, extract
- Download db4o, install
Db4objects.Db4o.CS.dll from the db4o install directory to the samples’ lib directory
- Open an Administrator command prompt and find the sample’s
bin folder. There is no feedback in case anything goes wrong (at best, the program crashes)
- Call the install routine by executing
emphess.db4oServer.exe -install. This will install the db4o server and the associated event log. The server will not be started automatically.
- Head over to
services.msc or your EventLog, where you should find a new entry!
- Edit the configuration file, ready to run!
- Don’t use this for anything production…
The configuration file looks like this
<?xml version="1.0" encoding="utf-8"?>
<add key="UseLocalFile" value="false" />
<!-- <add key="DatabaseLocalFileName" value="irrelevant"/> -->
<add key="DatabaseHost" value="localhost" />
<add key="DatabasePort" value="52354" />
<add key="DatabaseServerFileName" value="C:/Development/db4oServer/db/server.db4o" />
<add key="DatabaseUser" value="test" />
<add key="DatabasePassword" value="test" />
UseLocalFile setting is meant for clients so you can easily switch between file-based and network-based configurations. Since
db4oServer.Shared contains a small class that holds this configuration data, you can easily reuse the code in a different app, e.g. in the client.
There are some things I’d like to add over the next weeks, so I put the list here to give me some ‘pseudo-extrinsic motivation’… (yes, I like complicated words)
- Simple sample client
- Out-of-band data for backup, defragmentation commands, etc.
- Tests / Benchmarks
I hope you have any use for this.