Db4o with Castle Windsor and ASP.NET MVC 3.
July 31, 2011
This is how you can use Castle Windsor inversion of control container with Db4o. In this example I use the Db4o Server rather than the embedded version.
That’s because I want to be able to make multiple connections to Db4o in an ASP.NET MVC 3 application. So here is how it is. (It is rather short because it is so easy, or perhaps I’m missing things? Let me hear it if I do!).
First, open the package manager console and add the following packages:
And in the TestProject:
To get started with ASP.NET MVC 3 and Castle Windsor, just follow this excellent documentation. Don’t forget however to put this line in your web.config, in the System.Web section:
<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
I decided to use a generic repository. Since I use a Db4o Server (see the docs), connecting to that Windows Service returns a Db4o session (IObjectContainer). So I can use that session in the Repository.
This is also very testable.
But first let’s hook it up in Windsor:
Isn’t that simple?
We’ll use the IObjectContainer like this in the repository:
And finally, the Controller looks like this:
Please check out the source code here.
The Buttonfactory Db4o Windows Service
I know, this has been done before, here and here, and these people did it much better than I did, but I thought it would be nice to write a Db4o Windows service myself. Just as an exercise. I also wrote a WPF frontend so you can control the service easily:
You can download The ButtonFactory Db4o Windows Service and Configuration Tool here: Db4o.Service.Installer.Setup. Mind you, it is all very beta and I really need to test concurrency.
Here is the complete source code.