Skip to content

Instantly share code, notes, and snippets.

@msajid
Created January 19, 2020 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msajid/322a2c8d86bca6dc194ef7046bc202fe to your computer and use it in GitHub Desktop.
Save msajid/322a2c8d86bca6dc194ef7046bc202fe to your computer and use it in GitHub Desktop.
using Microsoft.Azure.Cosmos;
using System;
using System.Threading.Tasks;
namespace TestTransactionalBatchConsole
{
class Program
{
static async Task Main(string[] args)
{
var dbName = "BankDB";
var containerName = "AccountsContainer";
using (var cosmosClient = new CosmosClient("https://localhost:8081", "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="))
{
var dbCreated = await cosmosClient.CreateDatabaseIfNotExistsAsync(dbName, 400);
var containerResponse = await cosmosClient.GetDatabase(dbName).CreateContainerIfNotExistsAsync(containerName, "/AccountNumber");
var container = containerResponse.Container;
var metadata = new Metadata() { id = "0", AccountNumber = "12345", LastSequence = "2", AccountStatus = Status.Active };
var newAccountDetails = new AccountCreated() { id = "1", Holder = "Sajid", AccountNumber = "12345" };
var initialDepositDetailes = new DepositPerformed() { id = "2", Amount = 10, AccountNumber = "12345" };
var batchTransaction = container
.CreateTransactionalBatch(new PartitionKey(metadata.AccountNumber))
.CreateItem<Metadata>(metadata)
.CreateItem<AccountCreated>(newAccountDetails)
.CreateItem<DepositPerformed>(initialDepositDetailes);
var transactionResult = await batchTransaction.ExecuteAsync();
if (transactionResult.IsSuccessStatusCode)
{
var metadataCreated = transactionResult.GetOperationResultAtIndex<Metadata>(0);
var accountCreated = transactionResult.GetOperationResultAtIndex<AccountCreated>(1);
var initialDepositPerformed = transactionResult.GetOperationResultAtIndex<DepositPerformed>(2);
}
else
{
Console.WriteLine(transactionResult.ErrorMessage);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment