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:
public ActionResult Index() { var chassis = SessionFactory.Current.All<BladeChassis>(); return View(chassis); }
And this would be our Edit method (that retrieves an instance of the bladechassis to edit):
public ActionResult Edit(int id) { string cid = id.ToString(); var chassis = SessionFactory.Current.Single<BladeChassis>(x => x.ChassisID == cid); return View(chassis); }
And this is the actual altering the 'record' in the 'database':
[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(); } }
And here is the delete method:
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"); } }
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.
How to implement the security class for ASP.NET MVC 3 and using db4o for store information about users?
Why your example don’t run?
The RTM release of MVC3 uses
instead of
for the dynamic collection property. Changing the cshtml @View.Title references to @ViewBag.Title did the trick.
http://www.asp.net/mvc/mvc3#BM_Controller_Improvements
Actually, according to the above, it was
before. What were those @View.Title calls?
All the work, creating ISession, an implementation and a factory, but when you run it, you’ll see it will create a single Db4oSession and use that for all concurrent requests, never disposing it, and never creating a new one. Why? Because _current in SessionFactory will only populated once, and everything is static. Also the SessionFilter is never used, and contains a bug:
if (SessionFactory.Current == null)
{
SessionFactory.Current.Dispose();
}
This could create a null reference exception, when it tries to call Dispose(). So much for unit testing…