gpt4 book ai didi

c# - C# 中的 AWS DynamoDb 事务

转载 作者:行者123 更新时间:2023-12-04 15:42:06 25 4
gpt4 key购买 nike

我需要在 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;
}

I want to know how to implement Transaction using the Low Level API in C# for AWS DynamoDb?

谢谢!

最佳答案

IAAmazonDynamoDB.TransactWriteItemsAsync (TransactWriteItemsRequest) 怎么样?

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

关于c# - C# 中的 AWS DynamoDb 事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57495551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com