Create C# API Using Azure Cosmos Library V2 (Connecting with Singleton Pattern)

Adrian Hartanto
Bina Nusantara IT Division
3 min readDec 23, 2022

This Article is the second part when I create an SSIS API with Azure Cosmos Library. This article will mostly explain how the project is connected to the Cosmos Databases. I use a singleton pattern for this project to create the client connection.

Image from Unsplash by Alina Grubnyak

Why do I use a singleton pattern to connect to azure cosmos databases?

1. All Connection is disposable

When the connection is created, we use CosmosClient with a connection string. Therefore when a connection was used, CosmosClient had already been IDisposable. CosmosClient is thread-safe because it maintains a single instance per lifetime of our application. This is also the reason our application is efficient in connection management and performance.

So our first code is to initialize CosmosClient.

CosmosClient client = null;

You also can see that CosmosClient is an extended class from IDisposabale.

2. You can call the exact connection that you need without naming the container again.

Create a CosmosClient function that returns your HttpClient. You need to specify your connection string/endpoint and also you need to give the Cosmos Key in order to access your Database in Cosmos Database. In the below code, I define my Cosmos Endpoint and Cosmos Key in my connection string. Therefore I use GetSection function in the Program library.

Here is the code:

public CosmosClient GetClientDatabase()
{
if (client == null)
{
client = new CosmosClient(
accountEndpoint: Program.Configuration.GetSection("ConnectionStrings").GetSection("CosmosEndpoint").Value,
authKeyOrResourceToken: Program.Configuration.GetSection("ConnectionStrings").GetSection("CosmosKey").Value
);
}
return client;
}

Create your new instance if there is another database with a different endpoint and key you need to access. Please do not forget to specify it.

public CosmosClient GetClientDatabase2()
{
if (client == null)
{
client = new CosmosClient(
accountEndpoint: Program.Configuration.GetSection("ConnectionStrings").GetSection("CosmosEndpoint").Value,
authKeyOrResourceToken: Program.Configuration.GetSection("ConnectionStrings").GetSection("CosmosKey").Value
);
}
return client;
}

3. Initialized only once in the program when used in the first time.

In my first story about creating API using Azure Cosmos Library, I have already stated that we need a cosmos endpoint and also a cosmos key. But don’t forget about naming our container. So in the below code, we need to name our database name and container name to hit the exact container in Cosmos Databases.

private static Singleton singleton = new Singleton();
CosmosClient client = singleton.GetClientDatabase();

Database db = client.GetDatabase("YourDatabase");
Container ctr = db.GetContainer("YourContainerName");

Finally, you can read your item in Cosmos Databases. You can follow the below code to read a single item by id and partition key. The partition key in an item is a must-have so you need to create using “new PartitionKey()” like the code below.

Item readItem = await container.ReadItemAsync<Item>(
id: "someId",
partitionKey: new PartitionKey("PartitionKey-ItemId")
);

You also can read multiple items in ReadManyItemsAsync. Here is an example:

var data = (await container.ReadManyItemsAsync<Item>(listPair)).Resource;

In Conclusion

To summarize the reason I use a singleton pattern:

1. All Connection is disposable.

2. You can call the exact connection that you need without naming the container again.

3. Initialized only once in the program when used in the first time.

--

--