Going Global with Xamarin and Azure Cosmos DB

Mayur Tendulkar

Imagine that MyTeaCompany is a locally renowned business in Pune, India, supplying tea to the area from their ten shops within the city. They supply a huge variety of teas from Darjeeling to Assam and Thai to Korean, among others. They decide they want to take their business to the next level and create a worldwide presence on the internet.

Today, we’re going to take a look at how utilizing the Azure Cosmos DB database solution in their new Xamarin mobile app can help them leverage a wider customer-base on the global platform.

Building the Azure Cosmos DB solution

Azure Cosmos DB is a scalable, globally distributable solution for all your data requirements. It supports different models, including Graph, Table, and DocumentDb, with APIs available in different technologies, such as .NET, Python and Java. This makes it the perfect solution for all of MyTeaCompany’s data needs.

You can use the Azure Cosmos DB Emulator for developing and testing apps locally. However, for today’s blog post, we’ll use our Microsoft Azure Subscription.

Create an Azure Cosmos DB Account

Head to Azure Portal and create an Azure Cosmos DB account using the ‘+’ sign and navigating to Databases > Azure Cosmos DB. Once created, click on “Keys” and note the details, as those will be required in future steps.

It’s important to decide on the partition key, which helps to span data across servers or partitions, when creating a collection. It’s also important to carefully select your Request Units (RUs), which can play a major role in expected performance. The guide on the Request Unit Calculator is a great resource to help you gauge your requirements.

Xamarin.Forms Mobile App

Since we’re targeting multiple platforms, Xamarin.Forms is the perfect choice for MyTeaCompany, allowing us to maximize code sharing while still being able to use the native controls and features of each individual platform.

Create a Xamarin.Forms Project

In Visual Studio 2017, create a Xamarin.Forms solution by selecting New Project… > Cross-Platform > Cross-Platform App (Xamarin) > Blank / Shared Project. We’ll want to add the Microsoft.Azure.DocumentDB.Core NuGet package to the projects in order to use the package responsible for communicating with the Cosmos DB services.

Define Endpoint Constants

We need to define the endpoints in the Constants.cs file in order to connect the Xamarin.Forms application with Cosmos DB:

//copy details from your Keys section here
public static readonly string EndpointUri = "https://my-tea-company.documents.azure.com:443/";
public static readonly string PrimaryKey = "SKAF9lv43HeXicHgH-----------2yiFff7HPBOyFV0A==";
public static readonly string DatabaseName = "my-tea-company";
public static readonly string CollectionName = "my-tea-company";

Our model is StoreInfo which represents the MyTeaCompany store details. StoreInfoManager is the helper class that wraps around IDocumentDBService. This class helps us perform CRUD operations on our data. The DocumentDBService class implements IDocumentDBService and uses the details provided in the Constants.cs file to perform these CRUD operations.

class DocumentDBService : IDocumentDBService
   DocumentClient client;
   Uri collectionLink;
   public DocumentDBService()
      client = new DocumentClient(new Uri(Constants.EndpointUri), Constants.PrimaryKey);
      collectionLink = UriFactory.CreateDocumentCollectionUri(Constants.DatabaseName, Constants.CollectionName);

Going Global

After MyTeaCompany deploys their app using Azure Cosmos DB and Xamarin, they see a huge opportunity in Seattle, WA. They decide to open a store there and start serving customers through the existing app. With few changes the app can be configured to serve users in that geographic area. First, they needed to replicate the store and product data to the new Azure Regions, which can be done from the Azure Portal.

Once the the data is replicated, using ConnectionPolicy nearest Azure Regions could be set as PreferredLocations to keep the latency low.

public DocumentDBService()
   ConnectionPolicy connectionPolicy = new ConnectionPolicy();
   client = new DocumentClient(new Uri(Constants.EndpointUri), Constants.PrimaryKey, connectionPolicy);
   collectionLink = UriFactory.CreateDocumentCollectionUri(Constants.DatabaseName, Constants.CollectionName);

By making two simple changes, the entire solution was made available in an entirely new region. Tomorrow, if MyTeaCompany wanted to open a store in Europe, they could expand their reach to the region in just a couple of clicks.


Azure Cosmos DB makes it easy to build globally distributed, scalable, highly available and low latency data solutions, which can be easily integrated with Xamarin mobile apps. You can check out the completed sample described in this post here and the documentation for Azure Cosmos DB can be found here. You can also see our guide on Storing Data in a Document Database for more information.


Discussion is closed.

Feedback usabilla icon