3

我需要在数据库中实现C#一个AWS DynamoDb事务

我查看了官方网站,但没有看到任何 C# 示例

以下是我的不同 Db 操作。

 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;
         }

     }
 }

我正在使用低级 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;
        }

我想知道如何使用 C# 中的 Low Level API for AWS DynamoDb 实现事务?

谢谢!

4

1 回答 1

2

IAmazonDynamoDB.TransactWriteItemsAsync (TransactWriteItemsRequest) 怎么样?

https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/DynamoDBv2/MIDynamoDBTransactWriteItemsTransactWriteItemsRequest.html

于 2019-08-28T14:37:06.503 回答