3
votes

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#

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!

1
As far as I know, it doesn't exist yet. But, Java and C# are pretty close, you might be able to convert some of their code fairly easily. I've had to do that before and it worked well. github.com/awslabs/dynamodb-transactions/tree/master/src/main/…TrialAndError

1 Answers