DynamoDB将属性值转换为类型

发布于 2025-02-03 15:34:12 字数 693 浏览 1 评论 0 原文

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

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();

然后返回我的类型连接的列表,但是我明确映射了每个字段。是否有一个更简单的方法或辅助库可以进行映射?

Is there a more efficient way of converting dynamo db data into concrete types? For example, when I query the data everything is in:

List<Dictionary<string, AttributeValue>>

Is it possible to easily convert the type without having to loop through each item and doing this all manually?

For example I am doing:

    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();

This then returns a list of my type Connection however I am explicitly mapping each field. Is there an easier way or a helper library that can do the mapping?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晨敛清荷 2025-02-10 15:34:12

我发现的最简单方法是使用 document.fromttributemap 函数将其转换为 document 对象,然后使用再次将其转换为.NET类型DynamodBcontext.fromDocument 方法如下所示。

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

The easiest way I have found is to use the Document.FromAttributeMap function to convert it to a Document object and then convert it again to the .NET type using the DynamoDBContext.FromDocument method as shown below.

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);
    });
}
随风而去 2025-02-10 15:34:12

我认为您会使用高级.NET文档模型运气。它显示了更多自然数据类型。

I think you'll have luck with the higher-level .NET Document model. It presents more natural data types.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文