I need to implement a transaction in C#
with AWS DynamoDb
as database
I checked the official website but don't see any example with C#
- https://aws.amazon.com/blogs/aws/dynamodb-transaction-library/
- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/transaction-example.html
Below are my different Db operations.
public class DbHandler
{
private readonly IConfiguration _configuration;
private readonly RegionEndpoint _region;
private readonly AmazonDynamoDBClient _dynamoClient;
public DbHandler(IConfiguration configuration)
{
_configuration = configuration;
var awsSettings = configuration.GetSection("AWS:DynamoDb");
_region = RegionEndpoint.GetBySystemName(awsSettings["Region"]);
_dynamoClient = SetDynamoClient(awsSettings);
}
public async Task<EventTO> Add(EventTO eventObj)
{
try
{
//Db Operation#1
await _dynamoClient.PutItemAsync(
tableName: _configuration.GetSection("AWS:DynamoDb")["Table1"],
item: SetEventObject(eventObj));
//Db Operation#2
await _dynamoClient.PutItemAsync(tableName: _configuration.GetSection("AWS:DynamoDb")["Table2"], someotherObj);
return eventObj;
}
catch (Exception ex)
{
throw;
}
}
}
I am using the Low Level API
private Dictionary<string, AttributeValue> SetEventObject(EventTO eventObj)
{
//DynamoDb - Using Low Level API
var attributes = new Dictionary<string, AttributeValue>
{
//EventId
{
nameof(eventObj.EventId),
new AttributeValue
{
S =eventObj.EventId
}
},
//Event Title
{
nameof(eventObj.Title),
new AttributeValue
{
S = eventObj.Title.Trim()
}
}
};
return attributes;
}
I want to know how to implement Transaction using the Low Level API in C# for AWS DynamoDb?
Thanks!