DynamoDb将属性值转换为类型



是否有更有效的方法将dynamo数据库数据转换为具体类型?例如,当我查询数据时,所有内容都在:

List<Dictionary<string, AttributeValue>>

是否有可能轻松地转换类型,而不必遍历每个项目并手动执行这些操作?

例如:

return items.Select(item => new Connection
{
ConnectionId = Guid.Parse(item["connectionId"].S),
ClientId = item["clientId"].S,
ProviderId = item["providerId"].S,
Scopes = item["scopes"].SS.ToArray(),
CredentialsId = item["credentialsId"].S,
Evidences = ToEvidences(item["consentEvidences"].L)
})
.ToList();

然后返回我的类型Connection的列表,但是我显式地映射每个字段。是否有更简单的方法或辅助库来完成映射?

我认为您使用高级。net文档模型会很幸运。它提供了更自然的数据类型。

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DotNetSDKMidLevel.html

我发现最简单的方法是使用Document.FromAttributeMap函数将其转换为Document对象,然后使用DynamoDBContext.FromDocument方法将其再次转换为. net类型,如下所示。

public async Task<IEnumerable<WeatherForecast>> GetAll(string cityName)
{
var queryRequest = new QueryRequest()
{
TableName = nameof(WeatherForecast),
KeyConditionExpression = "CityName = :cityName",
ExpressionAttributeValues = new Dictionary<string, AttributeValue>()
{
{":cityName", new AttributeValue(cityName)},
}
};
var response = await _dynamoDbClient.QueryAsync(queryRequest);
return response.Items.Select(a =>
{
var doc = Document.FromAttributeMap(a);
return _dynamoDbContext.FromDocument<WeatherForecast>(doc);
});
}

最新更新