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:

public ActionResult Index()
            //instantiate the db4o objectcontainer:
            IObjectContainer db = MySingleInstance.Instance().

                //this is the tricky part.
                //We select all the Story objects from the db:
                var query = from Story s in db select s;
                //And we'll add that to a list:
                List allStories = new List();
                foreach (Story story in query)
                //We'll present the List to the View
                return View(allStories);
                //Close 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.

2 Replies to “Db4o and ASP.NET MVC 2 part 2”

  • Can anyone help me?
    I’m working in the example. I did step by step, but does not rotate. Is in trouble in class ISession (void dispose in class and StoryController (ActionResult public Index ()).

    • Hi Pedro,
      Sorry, my dayjob is keeping me away from Itslet at the moment. What exactly is the message you get when running the app?

Geef een reactie

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *

%d bloggers liken dit: