Back to basics: db4o and ASP.NET MVC 3

Let’s write an app with Db4o and ASP.NET MVC again! Check out this post first.

We’ve kept it simple & stupid: a model with two entities and a one-to-many relationship. It should set a base for most apps though:

A little background: in a data centre there are serverracks, with blade enclosures or blade chassis. In these enclosures, you can put servers. So, bladeservers go into one blade chassis. Here is the typical one to many relationship!

This is how I set up the solution in Visual Studio:

  • Core contains the business entities
  • Tasks is a console app to get things done quick and dirty
  • Test is for the Unit tests
  • Web is the ASP.NET MVC3 application (Empty) with Razor

So, just create a new class in Core and make it look like this:

Now go to the Db4o website quickly to grab db4o for .NET 4.0, version 8.0.160.14822 MSI. You may have to register first. Install the software.

Now open (add to the solution if you did not yet do so) the Web application and add some references:

I created the following folder structure:

Let’s first create the interface for the Unit of Work:

And since we’ll be using Db4o for our objectpersistence, let’s create a Db4oSession, that derives from ISession.

Next, we need to set up the mechanism that handles the connection to the database for us. This is the SessionFactory:

Now, we are ready to write our first tests:

I can run them successfully in Nunit, which is awesome.

And yes, this all is based on the weblog of my hero Rob Conery. I am so sorry.

Well, we’re almost there. We can actually write our controller methods now. Let’s add a BladeChassisController and add the Index method:

[code lang=”csharp”]
public ActionResult Index()
{
var chassis = SessionFactory.Current.All<BladeChassis>();
return View(chassis);

}
[/code]

And this would be our Edit method (that retrieves an instance of the bladechassis to edit):
[code lang=”csharp”]
public ActionResult Edit(int id)
{
string cid = id.ToString();
var chassis = SessionFactory.Current.Single<BladeChassis>(x => x.ChassisID == cid);
return View(chassis);
}
[/code]

And this is the actual altering the ‘record’ in the ‘database’:

[code lang=”csharp”]
[HttpPost]
public ActionResult Edit(int id, FormCollection values)
{
try
{
// TODO: Add update logic here
string cid = id.ToString();
var c = SessionFactory.Current.Single<BladeChassis>(x => x.ChassisID == cid);

UpdateModel(c);
SessionFactory.Current.Save(c);
SessionFactory.Current.CommitChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
[/code]

And here is the delete method:
[code lang=”csharp”]
public ActionResult Delete(int id)
{
try
{
string cid = id.ToString();
var chassis = SessionFactory.Current.Single<BladeChassis>(x => x.ChassisID == cid);
SessionFactory.Current.Delete(chassis);
return RedirectToAction("Index");
}
catch
{
return View("Index");
}
}
[/code]

It’s insanely simple. (Until you need to fill a DropDownList, but that’s a separate article).

You can download the example here.
Don’t forget to run the Console App in Tasks first, because it adds some data to the app.

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:

datienaarindex

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

datienaarindexview

And then we can scaffold the Index View like this:

index

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.

List

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.