Db4o with Castle Windsor and ASP.NET MVC 3.

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:

Install-Package db4o-cs-devel
Install-Package Castle.Windsor

And in the TestProject:

Install-Package Nunit

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.

Tooltip of the Month: How to detach and attach a sql database

Since the new Denali database really rocks my socks.dll,  it’s time to move the old databases from SQL 2008 R2 to Denali. It’s time for a quick and dirty database migration!

If you want to skip all the effort from backing up a database, copy it to another disc and restore it as a new database, then try this!

There are two ways to achieve a succesfull detach and attach, with management studio and with stored procedures, I will give both examples:
First make sure the database you are moving doesn’t have any related services running from the application or has active sessions to the database. ( sp_who Active)

from script:

We will be moving a database called kvdn, we need execute two relative simple queries. First one containing database name and whether we update the statistics. Update statistics will update and index columns and can increase the speed of your queries.

sp_detach_db [ @dbname = ] 'dbname'
[ , [ @skipchecks = ] 'skipchecks' ]

If ‘skipchecks’ is true, UPDATE STATISTICS will be skipped, to update the existing optimization statistics, change skipchecks to ‘false’.

EXEC sp_detach_db 'kvdn', 'true'

now we are going to attach in the new database environment
we need to provide database name and also filelocation for the mdf en ldf files

sp_attach_db [ @dbname = ] 'dbname',
[ @filename1 = ] 'filename_n' [ ,...16 ]

Open up the denali server and run this query to attach the kvdn database

EXEC sp_attach_db @dbname = 'kvdn',
@filename1 = 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\kvdn.mdf',
@filename2 = 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\kvdn_1.ldf'

We just succesfully moved the database.

and now for the gui way:

right click the database you want to detach and choose detach…

By default, the detach operation keeps your current optimization statistics, to update the existing optimization statistics, click the Update Statistics check box.

Now find the location if your database files, by default this will be “C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA”

move the files over to the new file location and open up the management studio where you want to have the database running.

right click database and select Attach…

Now insert the location of the database files

and that’s it, your database is up and running.

retrieving the database users:

I have seen examples where the users aren’t automatically moved along or stoped working with the attached database,  the easiest way is to just remove them, in case they did move and didn’t work and recreate them.

or re link them with the sp_change_users_login stored procedure:
EXEC sp_change_users_login 'Update_One', 'User', 'User'