Getting started with ASP.NET API with MongoLab part 3

This is the sequel to this post
We will be using MongoLab as a backend for our Web Api.

We are using the AddressBook again in this example, but for clarity we’re building it from scratch.

Creating the MongoLab database

Head over to MongoLab and create an account!

  • First create a new database. Make sure you choose the free plan. Name the database ‘addressbook’.
  • Click to create a new user.
  • Add a new collection (equivalent for a table) named persons.

Please take note of the connectionstring:

2013-11-01 08_12_03-MongoLab_ MongoDB-as-a-Service

Setting up the Web Api project in Visual Studio

Fire up Visual Studio 2013 and hit File -> New -> Project.
Choose Empty ASP.NET Web Application with a Web API and name the project AddressBook.

2013-11-01 08_07_48-New ASP.NET Project - AddressBook

We must install the Mongo Csharp Driver:

Now open App_Start\WebApiConfig.cs and add json formatting to it (see my previous post why):

Connecting to the MongoLab

Let’s not reinvent the wheel if you don’t have to. This article is a great introduction to Mongo and C#. I borrowed their MongoConnectionHandler.

The Person class

I want the Person_Id to be the same as the document id, so I can retrieve documents easily. I’m not sure though if this is best practice. But it works.

And here’s the POST method:

You can use the API with Fiddler.

2013-11-01 14_33_36-Fiddler Web Debugger

Look at the results it has returned:

2013-11-01 14_35_25-Fiddler Web Debugger

It returned the new URI with the new Id as you can see.

Now let’s check MongoLab, and lo and behold:

2013-11-01 14_39_53-MongoLab_ MongoDB-as-a-Service

Here’s the remainder of the CRUD methods:

So, that works.
Next time, let’s consume this WebApi with Angularjs.

And here’s all the code: here

I have changed ‘Person’ to ‘Contact’ in the sourcecode

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.