Let’s build an ASP.NET Core CRUD with NoSQL application.

I’ll be creating a basic CRUD HTTP API (CRUD is Create, Read, Update, Delete). This API will be operating on a gift wishlist: all the items I want you to buy me for my birthday. I won’t be building a UI, but you could use this API with the client-side framework of your choice (like React or Blazor, etc).

This first post will include all the ASP.NET Core NoSQL project setup and configuration database. The next posts will build out the actual CRUD endpoints.

Developer tools you’ll need

I’m using Visual Studio 2022 (Enterprise), but you should be able to follow along okay with JetBrains Rider, VSCode, or whatever you normally use. (If you need help, I’m happy to assist!)

I’m also using Couchbase Capella, which is the DBaaS (Database as a Service) Couchbase offering.

I’ve already signed up for a trial account. You can too, go to: https://cloud.couchbase.com/sign-up

The travel-sample bucket is already loaded when you deploy your trial cluster. But I’m using another bucket that I created called demo.

Configure Couchbase buckets on Capella DBaaS

Inside that bucket, there’s a _default scope, so I’ll use that. And inside that scope, I’ve created a wishlist collection. (Learn more about scopes and collections in Capella)

Configure Couchbase Collections on Capella DBaaS

CRUD credentials

You’ll also need to create database credentials with read/write access to everything in the demo bucket.

Configure Couchbase credentials on Capella DBaaS

Finally, whitelist the IP address that you will be compiling and running your app from so that you can connect.

Configure Couchbase IP whitelist on Capella DBaaS

Start an ASP.NET Core app

In Visual Studio, select File→New →Project→ASP.NET Core Web API. I called it AspNetCoreTutorial.

Setting up a ASP.NET project

I’m using .NET 6, and all the other defaults are fine (notice that OpenAPI, aka Swagger, is enabled).

Default settings in a ASP.NET project

This process creates a basic shell site.

Next, let’s use NuGet to add the Couchbase .NET SDK (CouchbaseNetClient).

Couchbase .NET SDK nuget

Notes on the .NET SDK

The Couchbase .NET SDK allows us to connect to a Couchbase Cluster. Couchbase is a distributed database, so typically there are several servers (called “nodes”) in a group (called a “cluster”) that all act together. A cluster has one or more “buckets”, which as you saw earlier, contains scopes, which contain collections, which then contain documents.

The data in a bucket is distributed amongst the nodes in the cluster, but it’s treated as a single logical entity by the SDK.

Within a collection, each document has a unique “key” and a JSON “value”.

For the trial version of Capella, there will only be a single node. However, this doesn’t affect your code: when nodes are added, the SDK is smart enough to be able to find them on its own.

In production, you will typically have at least three nodes, possibly a few buckets, and as many scopes and collections as you need (within reason).

Dependency Injection

I’m also going to add the Couchbase.Extensions.DependencyInjection NuGet package. This will provide extension methods to easily add Couchbase capabilities to ASP.NET Core’s built-in dependency injection.

(This package makes it easier to use Couchbase with ASP.NET Core, but it is optional).

Connect ASP.NET Core to Couchbase Capella

Let’s write some code in the ASP.NET app to connect to the Capella cluster.

In Program.cs, use the extension method on services:

(In this example, I’m hardcoding the connection information, but you can also use the configuration in appsettings.json instead).

One more step: when the application stops, I need it to release any Couchbase resources that .NET is using.

You can register code to execute on lifetime events, such as ApplicationStopped.

Now, ASP.NET Core’s dependency injection system will automatically inject the Couchbase objects when we want them.

Data Modeling

Before we write more code, let’s think about the data model. It’s going to be a very simple model: just the name of an item for the wishlist.

In a NoSQL document database, each piece of data has a key and a JSON value. The key can just be a GUID for our purposes (you could also make it a more meaningful key if you wanted). Each item on the wishlist has a “name“, so there’s going to be a name field.

I’ll create a C# class to represent an item:

A really simple model, but because Couchbase does not require a pre-defined schema, adding more fields can be as easy as adding them right here in the C# class.

Let’s go ahead and “prime” the database with a couple of Wishlist items.

Navigate to the wishlist collection in Couchbase Capella and add a couple of documents.

The first document will have a key “3ca6e87e-c3a6-4637-989d-33cbca5002b5“, and I’ll give it a name of “Skyline Chili T-Shirt“.

The second document will have a key “31c9cc33-8dfe-440c-bd1b-bb038939d2e0“, I’ll give it a name of “Joey Votto Jersey“.

Viewing Couchbase documents added to bucket

You can add other documents if you’d like, but we will eventually build an app where documents are added/changed via the ASP.NET Core app.

Controller for NoSQL CRUD actions

Next, create an ASP.NET Controller for my CRUD operations. I call it GiftsController.

In the constructor, specify an IBucketProvider parameter. This is an object that can be used to get a Bucket from the Couchbase DependencyInjection module. I will use it to get a Bucket object for the demo bucket.

You can execute the app at this point, just to verify that the connection works. There won’t be any endpoints yet (except the WeatherForecast example that Microsoft put in as an example).

What’s next?

We now have an ASP.NET Core project, connected to Couchbase Capella. In the next blog post, we’ll get into creating the actual CRUD endpoints.

In the meantime, you can:

Author

Posted by Matthew Groves

Matthew D. Groves 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 professionally 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 Senior Product Marketing Manager 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, Pro Microservices in .NET, a Pluralsight author, and a Microsoft MVP.

Leave a reply