.NetCore Firestore WebApi

.NetCore and Cloud Firestore

Pieter Linde

--

Cloud Firestore is a great way to store your data in a new and easy way. Firestore is a NoSQL database that is flexible, scalable and that can be used for web, mobile and other types of implementation. This tutorial will show you how to connect your C# project to a Firestore database.

Firebase Setup

Before we get started, the first thing we need to do is set up our Firebase environment. (https://console.firebase.google.com/)

If you don’t have a firebase project created already, you will have to create a new project. It is as easy as clicking “Add project”.

After you followed the initial setup steps and your new firebase project has been successfully created, we need to make sure your Cloud Firestore is up and running. Find the menu items called Cloud Firestore on the left side of your window, click on the button Create database

For this tutorial the rule set, we are using - Test Mode.

And that's it! Your firestore is ready for your data.

Create a new private key, this key will be used to connect your API project to the Firestore. (Take note of where this file is saved)

.Net Core API Setup

For this tutorial we are going to use ASP.NET Core (Web Api) to implement a simple way to communicate with a Firestore database.

Install the Nuget Package — google.cloud.firestore (https://github.com/googleapis/google-cloud-dotnet)

Model Setup

We will save Users to a Firestore table. The Base class will be the properties shared by all the models that you save inside your database. In the example we will only have one shared property - Id. Both your User Class and City Class must inherit from the Base class.

Also, don’t forget to add the [FirestoreData] above your models -> This indicates that the model can be saved in the database.

The [FirestoreProperty] is also important, only properties with this value assigned will be saved into the database. The property NotBeingSaved will not be saved in the database

(Github Code: https://github.com/pieterdlinde/firestore-test-application)

Interface Setup

The Firestore interface will give you a generic way to call your get’s, add’s and delete in a simple way. (Just wait, you will see)

Base Repository Setup

We will be able to use this generic class to access the Firestore environment.

In the section file path - You will need to reference the private key you downloaded from the Firebase website.

For this example, we are just hardcoding the path to the credentials JSON file, but you can also use the DbBuilder.

FirestoreDb firestormDb = new FirestoreDbBuilder{ProjectId = "projectName",JsonCredentials = "jsonCredentialsString"}.Build();

string filepath = "/Users/MyPCUser/Downloads/-firebase-adminsdk-ivk8q-d072fdf334.json";

https://gist.github.com/pieterdlinde/7facb9562df440d757cc2e58aa5ad80b

User Repository Setup

Remember to implement the FirestoreInterface and pass in the class User.

Now all you need to do is set up your controller and point it to the correct repository. I do recommend not calling your repositories directly from the controller, but rather using a Workflow and a Service Layer.

One tool that I recommend is - FluentValidation. https://fluentvalidation.net/

This will ensure that the data going into your database is valid and correct.

Github Code:

References: https://googleapis.github.io/google-cloud-dotnet/docs/Google.Cloud.Firestore/

https://developers.google.com/api-client-library/dotnet/apis/firestore/v1

Thank you for reading my first Medium post :)

Summary

If this helped you in any way or you just think this is cool, give it a 👍!

--

--