Db4o and ASP.NET MVC 2 part 2

In the last article I explained how to set up an ASP.NET MVC 2 project with a Db4o backend to create a weblog application. In the spirit of Domain Driven Design we created a single entity ‘Story’. We managed to create a separate class for database access. We did not however, implement the Unit of Work or the Repository pattern. We’ll get to that later. Let’s have a working application first and we’ll build in the abstractions later.

So, we created a StoryController in the Controllers folder and we implemented the Create method. After clicking Submit the method takes us to Index:


So we need to go to line 19 and right click on Index and click Add View, just like before:


And then we can scaffold the Index View like this:


Here is the code to list all the Story entries in the database:

Why am I copying the query result to a List? The answer is, if I don’t, the database closes before the page is displayed, so the page remains empty and we’ll get a DataBaseClosed Exception.

Now run the application (hit F5) and browse to http://localhost:portnr/Story.


And we see all our entries. If you click Create New, you’ll be directed to the form we made in part 1 and you can add another blog article. So basically the weblog application is ready, right?

The only thing is, it looks like shit. And I am no designer. So let’s grab a nice minimalist CSS template from the web. I googled ‘minimalist elegant css’ and came across Inkcover. The css of this site is a bit cluttered and it’s derived from a templating engine from Coldfusion I believe. But with a little hacking it came out quite nice.

Ik Zeg..

I’ll think I’ll have to notify the designers.

Next time I will cover validation at last. And perhaps upload the code.

Db4o and ASP.NET MVC 2 part 1

Today Scott Guthrie anounced the final release of ASP.NET MVC 2. So it’s a good time to give this a spin and check out the enhancements, like the new model validation and asynchronous controllers.

The best way to learn is to actually build an application. That’s what I would like to do here. I am going to build a blog application. For persistence I will use db4o, an object oriented database. Reasons for using Db40 is convenience. With Db4o, you don’t need to run SQL Server, which makes your code very portable and extremely easy to adjust to changing businessneeds.

The way I code this application is probably not from the books. It’s just how I think I should code. If you can think of any improvements please do not hesitate and tell us.

I’m going to call this application ‘IkZeg’. It’s a blog application, so we need a class for stories and for writers. We also like to enable comments. The classdiagram would be something like this:

Now install ASP.NET MVC 2 and download Db40 for .NET 3.5

Start Visual Studio (2008, I won’t use 2010 until it’s RTM-ed) and create a new ASP.NET MVC 2 Web Application: IkZeg.


I’m not adding a Test project because I just want to get going as fast as possible.

Then, rightclick the folder Models and add a new class and name it: Story.

Also add a Writer and a Comment class:

Now that we’ve created the model it’s time to dive into persistence with Db4o.

Data Access with Db4o

An object-oriented database stores objects directly in the database without having to map it to a relational structure. This is a major timesaver and it has lots of other advantages (and disadvantages) but that discussion is a bit out of scope. Db4o is just awesome to use.

Add references to dll’s from Db4o: Db40objects.Db4o, Db40objects.Db4o.CS and Db40objects.Db4o.Linq:


To implement persistence I followed Rob Conery because yes, he’s my hero. I learn an awful lot from him and from the Tekpub vids.

Data Access

To get started with Db4o is really simple. After adding the references you can start doing CRUD operations immediately, like so:

But that would mean we would be repeating ourselves continuously. With every CRUD operation, we would have to write the same piece of code and worry about closing the database. It would be nice to abstract these operations away. Also, maybe we would like to migrate to MySQL or Oracle later. So let’s take another approach.

Unit of Work

We’ll set up a Unit of Work. This takes care of CRUD methods. The value of using a Unit of Work pattern is to free the rest of your code from these concerns so that you can concentrate on business logic.


There’s a lot of funky generics in there. That’s good, because we need to implement this interface for all our ikZeg classes (Story, Writer and Comment).

Now let’s create a class that implements the ISession interface we just created:

Now create a class that inherits from Db4oSession and it all starts making sense:

And here is the class that holds the Db40 file- and connectioninfo:

Let’s use this

So, let’s create a story and show it on the website. To do that, I’ll need a controller and a view. Let’s start with the controller.

Right click the folder Controllers and click Add Controller.
Name the Controller StoryController and add action methods. Like this:


Add using statements for Db4o and the Models folder:

Just below the StoryController declare an IkZegSession:

Scroll down to line 63 or so to the Create method and make it look like this:

Now add the view. Right click the Create Method (along line 65) and click Add View.


Create a strongly-typed view. The data class is IkZeg.Models.Story. That’s the story class we made earlier.


Almost done now. Let’s take a look a this View though. In the folder Views, you have now a Story folder, with a file named Create.aspx. Open the file and remove the HTML fieldset (because I think it’s ugly). Also remove the Guid labels and textboxes and the Postdate labels and textboxes. We don’t need them as they are generated in the Create method in the controller.

Hit debug (or F5). The webbrowser starts with the standard webpage. Browse to: http://localhost:51305(or whatever portnumber)/Story/Create. And there we are. It’s a bit ugly but we will work at the layout later.

After clicking Create you’ll get a Yellow Screen of Death, which is okay, as we did not implement the Index method yet. I will cover that (and validation) in part 2.