Azure cosmos DB and Xamarin forms

Why Azure Cosmos DB?

Azure Cosmos DB is the recommended backend database for Xamarin mobile apps due to its speed, scalability and many other cool features such as geospatial support. For a mobile developer like me, Azure Cosmos DB is appealing as there is no middle-tier i.e no  Api work is required as that is all handled in the Azure Cosmos DB architecture.

Beginners guide to Azure Cosmos DB

Integrating Azure Cosmos DB to Xamarin app solution is simple. All we need it two things:

  1. Create the Azure Cosmos DB in azure portal
  2. Integrate it within out Xamarin app

Creating Azure Cosmos DB

There are two ways to start creating Azure Cosmos DB in cloud. We could mock the Azure Cosmos DB locally using the Azure Cosmos Emulator. This is quite a nifty tool as we do not need to worry about creating azure portal accounts or any paid subscriptions. Fortunately, at the time of writing this, azure provided a free tier which gets you the first 400 RU/s and 5 GB of storage for free in an account. Learn more about free tier.

To begin with, you will need an azure subscription. You can create a free azure portal account if you don’t have one. Thereafter, follow the below summaried steps or for more detailed instructions, check out Azure Cosmos DB quick start

  1.  Login to azure portal 
  2. Click on create resource 
  3. Search for Azure Cosmos DB
  4. Create Azure Cosmos DB account
  5. Review and create 
  6. Wait for deployment to complete 

Add a container

Containers are more like tables in a normal SQL server database with the key difference begin, they do not conform to a strict schema structure. Follow these steps to create a container:

  1. Select the newly created resource 
  2. Go to Data explorer in the left panel
  3. Click New container
  4. Enter your database name
  5. Enter container name
  6. Enter Partitioning key

Obtaining API Keys

To access the Azure Cosmos DB from our Xamarin app, we need the URL and the primary key values from the portal which can be retrieved from:

  1. Select Keys from the left panel 
  2. Select Read-write keys 
  3. Copy URL value 
  4. Copy Primary key value

Initially, we will hard code these values in the Xamarin app as explained below. Once we are happy with the basic database setup we will dive into authenticating users to securely obtain these values.

Integrating Azure Cosmos DB to our Xamarin app

  1. Install Microsoft.Azure.DocumentDb.Core Nuget package to the Xamarin app solution.
  2. Create an AzureCosmosDB class in a services folder and enter the database name, collection name, API URL and primary Keys values as described in the previous steps. These values are needed to initialize the document client which is used to configure and execute requests against the Azure Cosmos DB account

DocumentClient

Create the CRUD operations methods as shown below

GetAsync

Crud operations

Troubleshooting

Partition Key value must be supplied for this operation – When you encounter this error during a delete operation, ensure you have both the item primary Id and the partition key value included the the query. The Partition Key can be included through the RequestOptions parameter as shown below:

Partition Key

Conclusion

And that’s it! You can now use these CRUD operations to add, edit and delete items to your Azure Cosmos DB. To view the data in cloud:

  1. Login to the azure portal,
  2. Select the azure cosmos db resource
  3. Go to Data Explorer
  4. Click Edit Filter
  5. Enter a custom SQL query or Click Apply filter. 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s