Couchbase with Windows and .NET – Part 2

Editor’s note: The following post was written by Visual Studio and Development Technologies MVP Matt Groves as part of our Technical Tuesday series with support from his technical editor, Visual Studio and Development Technologies MVP Travis Smith.

In this three part series, we’re going to look at the basics of interacting with Couchbase for .NET developers on Windows. We’ll start with the basics, and build towards a "vertical" slice of a complete ASP.NET MVC app on the .NET 4.x framework. For a deeper dive, please check out my blog posts on Couchbase and the Couchbase Developer Portal.

In this first part, we installed Couchbase Server and went over the basics of how it works.

In the second part, we’ll look at using ASP.NET with Couchbase Server.

In the final part, we’ll implement all the CRUD functionality in an ASP.NET application.

ASP.NET MVC

Now that we’ve got some lingo out the way, let’s start a new ASP.NET MVC project, add the Couchbase SDK to it with NuGet, and get the infrastructure in place to start using Couchbase.

Let’s start in Visual Studio with a File→New, and select ASP.NET Web Application, then select "MVC". I’m going to assume you have some familiarity with ASP.NET MVC (we’re using .NET 4.x, but a .NET Core Couchbase SDK is coming soon).

Installing the Couchbase client library

The first thing we’ll need to do is add the Couchbase .NET client. We can do this with the NuGet UI by right-clicking on "References", clicking "Manage NuGet Packages", clicking "Browse", and then searching for "CouchbaseNetClient". (We could search for "Linq2Couchbase" instead. Installing that will also install CouchbaseNetClient, but we won’t actually be using Linq2Couchbase until later).

If you prefer the NuGet command line, then open up the Package Manager Console, and type Install-Package CouchbaseNetClient.

Getting ASP.NET to talk to a Couchbase cluster

Now let’s setup the ASP.NET app to be able to connect to Couchbase. The first thing we need to do is locate the Couchbase Cluster. The best place to do this is in the Global.asax.cs when the application starts. At a minimum, we need to specify one node in the cluster, and give that to the ClusterHelper. We only need this in Application_Start. When the application ends, it’s a good idea to close the ClusterHelper.

Using the IBucket in a controller

Just to show that this works, go ahead and add IBucket to a constructor of a controller, say TestController.

(In the long run, we don’t want an IBucket directly in MVC controllers, more on that later).

Next, let’s add a document to the bucket, directly in Couchbase Console. Use anything for a key, but make a note of it.

Now, let’s add an action to TestController. It will get the document based on the key, and write the document values in the response.

doc.Value is of type dynamic, so make sure that the fields (in this case, "name" and "address") match up to the JSON document in the bucket. Run the MVC site in a browser, and we should see something like this:

Congratulations, we’ve successfully written an ASP.NET site that uses Couchbase!

Laying the groundwork for Linq2Couchbase

Let’s do some refactoring before introducing Linq2Couchbase. We’ll move Couchbase out of the Controller and put it into a very basic repository class.

Moving Couchbase out of the Controller

The Controller’s job is to direct traffic: take incoming requests, hand them to a model, and then give the results to the view. To follow the SOLID principles (specifically the Single Responsibility Principle), data access should be somewhere in a "model" and not the controller.

The first step is to refactor the existing code. We can keep the 'really simple example', but let’s move it to a method in another class. Here is the refactored HomeController and the new PersonRepository:

Now, HomeController no longer depends directly on Couchbase.

Refactoring to use a Person class

In the above example, we’re using a dynamic object. dynamic is great for some situations, but in this case, it would be a good idea to come up with a more concrete definition of what a "Person" is. We can do this with a C# class.

We’ll also update the PersonRepository to use this class.

While we’re at it, we’re going to take some steps to make this more of a proper MVC app. Instead of returning Content(), We’re going to make the Index action return a View, and we’re going to pass it a list of Person objects. We’ll create an Index.cshtml file, which will delegate to a partial of _person.cshtml. We’re also going to drop in a layout that uses Bootstrap. This last part is gratuitous, but it will make the app look nicer.

New Index action:

Index.cshtml:

Now it looks a little nicer. Additionally, we’ll be able to show a whole list of Person documents later.

That’s it for now

I’ve put the full source code for this example on Github. Note that this source code represents the final product, so if you check it out now you will have a head start on the next and final blog post in the series.

Matt is a guy who loves to code. It doesn't matter if it's C#, jQuery, or PHP: he'll submit pull requests for anything. He has been coding ever since he wrote a QuickBASIC point-of-sale app for his parent's pizza shop back in the 90s. He currently works as a Developer Advocate for Couchbase. His free time is spent with his family, watching the Reds, and getting involved in the developer community. He is the author of AOP in .NET (published by Manning).